The following source code is from WebKit:
Differences between var d = Date () and VAR d = new date
The former is just a normal function call and is executed through jsvalue. getcalldata, while the latter is to create an object through
Jsvalue. getconstructdata to execute
Therefore, there may be an essential difference between the two. The difference lies in the location to which calldata. Native. function points when a call is returned.
The Analysis of JavaScript core shows that the former only returns a string, while the latter creates a date object.
Code for the former:
Static jsvalueptr calldate (execstate * exec, jsobject *, jsvalueptr,
Const Arglist &)
{
Dbug_enter ();
Time_t localtime = time (0 );
TM localtm;
Getlocaltime (& localtime, & localtm );
Gregoriandatetime TS (localtm );
Return jsnontrivialstring (exec, formatdate (TS) + "" +
Formattime (TS, false ));
}
Calltype dateconstructor: getcalldata (calldata & calldata)
{
Dbug_enter ();
Calldata. Native. Function = calldate;
Return calltypehost;
}
Code corresponding to the latter:
Static jsobject * constructwithdateconstructor (execstate * exec,
Jsobject *, const Arglist & ARGs)
{
Dbug_enter ();
Return constructdate (exec, argS );
}
Constructtype dateconstructor: getconstructdata (constructdata &
Constructdata)
{
Dbug_enter ();
Constructdata. Native. Function = constructwithdateconstructor;
Return constructtypehost;
}
It can also be analyzed that VaR A = array () and var a = new array () are the same
Static jsobject * constructwitharrayconstructor (execstate * exec,
Jsobject *, const Arglist & ARGs)
{
Dbug_enter ();
Return constructarraywithsizequirk (exec, argS );
}
// ECMA 15.4.2
Constructtype arrayconstructor: getconstructdata (constructdata &
Constructdata)
{
Dbug_enter ();
Constructdata. Native. Function = constructwitharrayconstructor;
Return constructtypehost;
}
Static jsvalueptr callarrayconstructor (execstate * exec, jsobject *,
Jsvalueptr, const Arglist & ARGs)
{
Dbug_enter ();
Return constructarraywithsizequirk (exec, argS );
}
// ECMA 15.6.1
Calltype arrayconstructor: getcalldata (calldata & calldata)
{
Dbug_enter ();
// Equivalent to 'new array (....)'
Calldata. Native. Function = callarrayconstructor;
Return calltypehost;
}