javascript--the reference pass of the object argument

Source: Internet
Author: User

Today, there is a problem, how to affect the parameter changes outside the function, such as:

<Script>    varmyname= "Wood";    A (myname);    document.write (myname); functionA (n) {n= "Yao"; }</Script>

The output is also wood, which shows that when myname passes in a function, it is equivalent to having a copy of the myname in the body of the function, the value of which is equal to myname, and what is done to it in the function body is performed on that copy.

But the situation is different, when the passed parameter is an array, an object , changes to the parameters in the function body are reflected on the original variable.

<Script>    varmyname= ["Wood"];    A (myname); document.write (myname[0]); functionA (n) {n[0] = "Yao"; }</Script>

As you can see, the first element of the Friut array has been changed in the above code.

Here is an example of an object:

<Script>    varmyname={name1:"Wood"};    A (myname);    document.write (MYNAME.NAME1); functionA (n) {n.name1= "Yao"; }</Script>

It is obvious that the changes in the parameters of the function body affect the original variable, which is different from the usual parameter. Special attention is required.

But, when assigning a value to an incoming array or object in the body of a function, the change is not reflected in the original variable outside the function body!

Please see:

<Script>    varmyname={name1:"Wood"};    A (myname);    document.write (MYNAME.NAME1); functionA (n) {n={name1:"Yao"}; }</Script>

You must think that the value of the MyName variable's Name1 property has been changed to ' Yao ' after executing a (), as the changes inside the above function reflect the theory of the original variable. But the results are a bit hard to accept.

The reason for this is that when an assignment is used within a function body, a variable named P is created. This p is a variable inside the function, it is assigned to the function in the body of course, the outside of the myname is still the original myname.

The difference between this step and the original code is whether the parameter is assigned a new value in the body of the function, or the property of the parameter or the element of the array is changed .

Here we re-implement the example of a clock number format output by passing the object:

<Script>    varMyTime=Self.setinterval (function() {getTime (); },  +); //alert ("OK");    functionGetTime () {varTimer= NewDate (); varT={h:timer.gethours (), M:timer.getminutes (), S:timer.getseconds () }            //The time object T, passed into the function checktime (), changes the value in the object directly in Checktime ().             //Without having to receive the return value and then assign a valuechecktime (t); document.getElementById ("Timer"). InnerHTML=t.h+ ":" +T.M+ ":" +T.s; }    functionChecktime (i) {if(i.h< Ten) {i.h= "0" +i.h; }        if(I.M< Ten) {I.M= "0" +I.M; }        if(I.s< Ten) {I.s= "0" +I.s; }    }</Script>

The example uses the SetInterval () function to periodically invoke a refresh event, or it can be implemented by recursive invocation of settimeout () in gettime ().

javascript--the reference pass of the object argument

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.