Learning Notes: JavaScript method ——— parameters for all functions in ECMAScript are passed by value

Source: Internet
Author: User
Tags call by reference

We treat the named parameter (arguments) as a local variable, and when passing a primitive type value to a parameter, passing a copy like a copy of a primitive type variable, the change inside the function does not affect the external base type value. Such as:

1 function add10 (num) {2num + = ten; 3 return num; 4 }5var count = ten; 6 var result = ADD10 (count); 7 alert (count);// 8 alert (result); 20

When a value of a reference type is passed to a parameter, the address of the value in memory is copied to a local variable, so the change of this local variable will reflect the outer part of the function, for example:

function AddName (obj) {    obj.name = "Jewl";} var somebody = new Object (); AddName (somebody); Console.log (Somebody.name)    ; "Jewl"

This example is actually very disturbing, because it is more like passing by reference, speaking of the way of delivery, generally divided into two kinds:

1. Pass as value (call by value): The function's formal parameter is a copy of the argument that was passed when it was called. Modifying the value of a parameter does not affect the argument.

2. Pass by reference (call by reference): the formal parameter of a function receives an implicit reference to an argument, not a copy. This means that if the value of a function parameter is modified, the argument is also modified. Both points to the same value.

To prove that the reference type is also passed by value, we can take a look at the following example:

function AddName (obj) {    obj.name = "JEWL";    obj = new Object ();    Obj.name = "Kancy";} var somebody = new Object (); AddName (somebody); Console.log (somebody.name)    //"JEWL"

The difference from the previous example is that this example assigns a new object to the parameter, and if it is passed by reference, then the name of the somebody will change, obviously not. When you assign a value to obj inside a function, the variable reference becomes a local variable that is destroyed when the function finishes executing.

In order to deepen understanding, I will look at JS in the reference type value of the storage, in JS, the reference type value is stored in memory of the object, JavaScript does not allow direct access to the memory location, that is, the object can not directly manipulate the memory space, when manipulating the object, is actually a reference to the manipulation object and not the actual object. The value of a reference type is accessed by reference. So changing one of these variables will affect the other.

var obj1 = new Object ();
var obj3 = new Object ();
var obj2 = obj1;
Obj1.name = "JEWL";
Obj3.name = "Kancy";
Console.log (obj1.name);//"JEWL"
Console.log (Obj2.name); "Jewl"
Obj1 = obj3;
Console.log (obj1.name);//"Kancy"
Console.log (obj2.name);//"JEWL"
Console.log (obj3.name);//"Kancy"

When we assign obj1 to Obj3, Obj1 points to and obj3 the same object, and Obj2 still points to the previous object. As a result, the pass-through process is similar to this one, but is not referenced by reference, (as explained above), so the parameter passing in JavaScript is attributed to the passing of the value.

Learning Notes: JavaScript method ——— parameters for all functions in ECMAScript are passed by value

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.