Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

function now(){
    return +new Date;
}

questions :

  1. what does the plus sign mean?
  2. when can you create a new object with a constructor function but without the following parentheses, such as new Date but not new Date()

great thanks!

like image 613
user133580 Avatar asked Dec 23 '22 10:12

user133580


1 Answers

1 . The plus sign is the unary + operator.

That expression is equivalent to cast the Date object to number:

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

2 . If you don't add the parenthesis, the new operator will call the object type (Date) parameterlessly

like image 69
Christian C. Salvadó Avatar answered Apr 11 '23 14:04

Christian C. Salvadó