Value passing and reference passing problems for Javascript Date types

Source: Internet
Author: User

Creates a new variable of type date D1.

<span style= "Font-family:microsoft yahei;font-size:14px;" >var D1 = new Date (); alert (D1);</span>

The post-run page will pop up:

Fri Sep 16:40:50 gmt+0800


Create a new variable d2, assign the value of D1 to D2, set the D2 year to 2013,

<span style= "Font-family:microsoft yahei;font-size:14px;" >var D2 = D1;d2.setfullyear ("n"); alert (D1 + "/" + D2);</span>

The post-run page will pop up:

Thu Sep 16:40:50 gmt+0800

Thu Sep 16:40:50 gmt+0800


Even if the D2 is declared as a date type, the result is still the same.

<span style= "Font-family:microsoft yahei;font-size:14px;" >var D1 = new Date (), alert (D1), <span style= "color: #FF0000;" >var D2 = new Date (), </span>d2 = D1;d2.setfullyear ("n"), alert (D1 + "/" + D2);</span>

indicates that this direct assignment of the date type becomes a reference value, D1, D2 two variables to the same date object.

It is important to use the "=" symbol to assign values directly to an object in such a way that the result is inconsistent with what you expected, resulting in an incorrect operation.






Value passing and reference passing problems for Javascript Date types

Related Article

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.