JavaScript Object parameter reference transfer _ javascript skills

Source: Internet
Author: User
This article mainly introduces the reference and transfer of JavaScript Object parameters. If you are interested, you can refer to a problem encountered today and how to change the parameters to the external function, such:

《script》  var myname = "wood";  A(myname);  document.write(myname);  function A(n) {    n = "Yao";  }《script》

The output result is still wood, indicating that after myname is passed into function A, there is A copy of myname in the function body. The value of this copy is myname, the subsequent operations in the function body are performed on this copy.

However, when the input parameters are arrays and objects, changes made to the parameters in the function body are reflected in the original variables.

《script》  var myname = ["wood"];  A(myname);  document.write(myname[0]);  function A(n) {    n[0] = "Yao";  }《script》

We can see that the first element of the friut array has been changed in the code above.

The following is an example of an object:

《script》  var myname = {name1:"wood"};  A(myname);  document.write(myname.name1);  function A(n) {    n.name1 = "Yao";  }《script》

It can be seen that the changes to parameters in the function body have an effect on the original variables, which is essentially different from passing parameters. Special attention is required.

But, when the input array or object is assigned a value in the function body, this change will not be reflected in the original variable in the function body!

See:

《script》  var myname = {name1:"wood"};  A(myname);  document.write(myname.name1);  function A(n) {    n = {name1:"Yao"};  }《script》

According to the theory that the changes in the above function will reflect the original variable, you must think that after executing A (), the value of the name1 attribute of the myname variable has changed to 'yunao. However, the results are somewhat unacceptable.

The reason is that when the assignment operation is used in the function body, the system creates a variable named p. This p is a variable in the function. Of course, assigning values to it only takes effect in the function body. The external myname is still the original myname.

The difference between this step and the operation of the original code lies only in whether to add a new value to the parameter in the function body or change the attribute of the parameter or the element of the array.

The following example shows how to re-format the output of a clock number by passing an object:

Script var mytime = self. setInterval (function () {getTime ();}, 1000); // alert ("OK"); function getTime () {var timer = new Date (); var t = {h: timer. getHours (), m: timer. getMinutes (), s: timer. getSeconds ()} // pass the time object t into the checkTime () function and directly change the value of the object in checkTime. // No need to receive the returned value and then assign the value checkTime (t); document. getElementById ("timer "). innerHTML = t. h + ":" + t. m + ":" + t. s;} function checkTime (I) {if (I. h <10) {I. h = "0" + I. h;} if (I. m <10) {I. m = "0" + I. m;} if (I. s <10) {I. s = "0" + I. s ;}} script

In this example, the setInterval () function is used to call the refresh event regularly. You can also use setTimeout () to call the refresh event recursively in getTime.

The above is all the content of this article, and I hope it will help you learn javascript programming.

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.