JavaScript Advanced Object-oriented (12)--attributes that reference type value types are passed as parameters

Source: Internet
Author: User

Description (2017-4-2-18:27:11):

1. As an argument to a function, a copy of the function's data is passed to the parameter in the definition of the function.

The function foo () does two things at the time of invocation:

(1) When the function is called, it is necessary to first copy the data in the parameter, that is, the number 123 copies one copy.

(2) Jump to the definition of the function (function body), before this completes the function assignment, namely num=123.

(3) Formal entry function, ready to execute each sentence of the function.

1     function foo (num) {} 2     var a = 123; 3     Foo (a);

2. A value type is a feature that is passed as a function parameter, and the function is two different variables outside the function, only the value is equal.

3. A reference type is a feature passed as a function parameter, and within a function is two different variables outside the function, but points to the same object.

* Therefore inside the function allows modifying the data of an object outside the function.

Example 1: Because P1 is not defined first, there is no P1 object. Error: Cannot set the Name property of "undefined".

1<script type= "Text/javascript" >2     varo = {name: "Zhang San", Age:19, Gender: "Male"};3     //the second method of copy copy, with parameters (the first method is return value)4O.copy =function(obj) {5          for(varKinch  This){6OBJ[K] = This[K];7         }8     };9     varP1;Ten o.copy (p1); One</script>

In the example 2:copy function, obj = {} is set. No error, but watch inside is still undefined. Because obj has re-pointed to the empty object {}, it has nothing to do with P1, so you can successfully assign the name attribute to obj, so no error (except that the function is running, the data inside the function is not referenced, the data is deleted and the memory is freed). But P1 is still undefined.

1<script type= "Text/javascript" >2     varo = {name: "Zhang San", Age:19, Gender: "Male"};3     //the second method of copy copy, with parameters (the first method is return value)4O.copy =function(obj) {5         //set obj here equals an empty object, sinkhole!!! 6         varobj = {};7          for(varKinch  This){8OBJ[K] = This[K];9         }Ten     }; One     varP1; A o.copy (p1); -</script>

Example 3: The correct procedure, which declares the P1 bit directly outside of an empty object. This parameter, obj, also points to the same empty object, as long as obj changes, and the P1 changes.

1<script type= "Text/javascript" >2     varo = {name: "Zhang San", Age:19, Gender: "Male"};3     //the second method of copy copy, with parameters (the first method is return value)4O.copy =function(obj) {5          for(varKinch  This){6OBJ[K] = This[K];7         }8     };9     //The correct procedure, which declares directly outside the P1 bit an empty object. Ten     varP1 = {}; One o.copy (p1); A</script>

Summarize:

the parameter of the function is actually an assignment process , but the "assignment" is the direct assignment, and the "parameter" is invisible assignment.

JavaScript Advanced Object-oriented (12)--attributes that reference type value types are passed as parameters

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.