What is the role of the plus sign (+) in the following function, which is an excerpt from jquery source code

function now(){ return +new Date; } 

questions:

  • What does the plus sign mean?
  • when you can create a new object using the constructor function, but without the following brackets, for example new Date , but not new Date()

Thank you so much!

+4
source share
2 answers

1. The plus sign is the unary + operator.

This expression is equivalent to having a Date object number:

 function now(){ return Number(new Date); } 

2. If you do not add a parenthesis, the new operator will easily call the object type (Date)

+8
source
  • Using the plus sign converts the date to a number (the number of milliseconds since January 1, 1970).

  • You can do this when there are no parameters, although you can enable them even for readability.

+3
source

All Articles