Differences between var d = Date () and VAR d = new date

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.