JavaScript Object parameter reference passing _javascript tips

Source: Internet
Author: User
Tags setinterval

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

<script>
  var myname = "Wood";
  A (myname);
  document.write (myname);

  function A (n) {
    n = ' Yao ';
  }
</script>

The output is wood, which means that when myname passes in a function, it is equivalent to having a copy of the myname in the function body, the value of which is equal to MyName, and then the operation in the function body is performed on the copy.

However, when the incoming argument is an array, an object, the changes to the parameters in the function body are reflected on the original variable.

<script>
  var myname = ["Wood"];
  A (myname);
  document.write (Myname[0]);

  function A (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>
  var myname = {name1: "Wood"};
  A (myname);
  document.write (myname.name1);

  function A (n) {
    n.name1 = "Yao";
  }
</script>

It is obvious that the changes in the parameters in the body of the function affect the original variable, which is different from the normal reference. Need special attention.

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

Please see:

<script>
  var myname = {name1: "Wood"};
  A (myname);
  document.write (myname.name1);

  function A (n) {
    n = {name1: ' Yao '};
  }
</script>

As changes in the above function reflect the theory of the original variable, you must think that the value of the name1 attribute of the myname variable after the completion of a () has become ' Yao '. But the results are a bit hard to accept.

The reason is that when you use an assignment in a function body, the system creates a variable with a variable named P. This p is a variable inside the function, and assigning it to the function, of course, only works in the body, outside the myname or the original myname.

This step differs from the original code simply by assigning a new value to the parameter in the body of the function or by changing the attributes of the parameter or the elements of the array.

Below we use the way of passing objects, to realize a clock number formatted output example:

<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 ()
      }
      ///Time object T, Pass in the function Checktime () and change the value in the object directly in Checktime ().
      //without having to receive the return value and then assign the value
    checktime (t);
    document.getElementById ("Timer"). InnerHTML = T.h + ":" + t.m + ":" + T.s;
  }

  function Checktime (i) {
    if (I.h <) {
      i.h = "0" + i.h;
    }
    if (I.M <) {
      I.M = "0" + i.m;
    }
    if (I.s <) {
      I.S = "0" + I.s;
    }
  }
</script>

The example uses the SetInterval () function to invoke the refresh event at timed intervals, or it can be implemented by recursive calls in GetTime () in settimeout ().

The above is the entire content of this article, I hope to learn JavaScript program to help you.

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.