Home > Web Front-end > JS Tutorial > Js parameter passing and variable copying

Js parameter passing and variable copying

一个新手
Release: 2017-10-26 10:05:06
Original
1830 people have browsed it

  ECMAScript variables may contain values ​​of two different data types: primitive type values ​​and reference type values. Primitive type values ​​refer to simple data segments, while reference type values ​​refer to objects that may be composed of multiple values.

5 basic data types: Undefined, Null, Boolean, Number and String. These 5 basic data types are accessed by value because the actual value stored in the variable can be manipulated. ES6 has one more Symbol type.

 The value of a reference type is an object stored in memory. JavaScript does not allow direct access to the location in the memory, which means that the memory space of the object cannot be directly manipulated. When you operate on an object, you are actually operating on a reference to the object rather than the actual object. For this purpose, values ​​of reference types are accessed by reference.

Copy variable value

  • [Copy variable value] Copying the value of a basic type variable will create a new value on the variable object, and then copy the value to The location where the new variable is assigned. Operations on any variable value do not affect each other.

  • [Copy reference pointer] Copying the value of a reference type variable will also copy the value stored in the variable object into the space allocated by the new variable. The difference is this The copy of the value is actually a pointer to an object stored in the heap. After copying, both variables will actually refer to the same object. Therefore, changing any variable will affect another variable.

Parameter passing

The parameters of all functions in ECMAScript are passed by value. There are two ways to access variables: by value and by reference, while parameters can only be passed by value.

  • Basic type parameter passing: What is passed to the function is a copy of the value, and modifications to it in the function are not visible externally.

var a = 1;
var b = 2;
function change(a, b) {
    var c = a;
    a = b;
    b = c;
    console.log(a);    //2
    console.log(b);    //1
}
change(a, b);
console.log(a);    //1
console.log(b);    //2
Copy after login
  • Reference type parameter passing: What is passed to the function is a reference to the value. The modification of its properties in the function is externally visible, but it is overwritten with a new reference. Then it is not visible externally

var a = [1, 2, 3];
var b = [5, 6];
function change(a,b) {
  a[0] = 4;    //对其属性的修改外部可见 
  var c = a;
  a = b;      //用新引用覆盖
  b = c;
  console.log(a);  //"5,6"        
  console.log(b);  //"4,2,3"
}
change(a,b);
console.log(a);    //"4,2,3"
console.log(b);    //"5,6"
Copy after login

 a and b are variables in the change function. When calling the function, the references of a and b are assigned to these two variables, but they cannot Change a, b in the global. Because overwriting with a new reference is not visible to the outside, because the function only gets the reference and has no power to change the reference.

var a = [1, 2, 3];
var b = [5, 6];
function change() {
  var c = a;
  a[0] = 4;    //对其属性的修改外部可见 
  a = b;      //用新引用覆盖
  b = c;
}
change(a,b);
console.log(a);  //"5,6" 
console.log(b);  //"4,2,3"
Copy after login

Because js does not have a block-level scope, it cannot find variable a in change, and b will consciously go to the upper layer to find it, so a and b here are references to global variables.


☞☞☞Deep dive into the JavaScript series☜☜☜

  ECMAScript variables may contain values ​​of two different data types: basic type values ​​and reference type values. Primitive type values ​​refer to simple data segments, while reference type values ​​refer to objects that may be composed of multiple values.

5 basic data types: Undefined, Null, Boolean, Number and String. These 5 basic data types are accessed by value because the actual value stored in the variable can be manipulated. ES6 has one more Symbol type.

 The value of a reference type is an object stored in memory. JavaScript does not allow direct access to the location in the memory, which means that the memory space of the object cannot be directly manipulated. When you manipulate an object, you are actually manipulating a reference to the object rather than the actual object. For this purpose, values ​​of reference types are accessed by reference.

Copy variable value

  • [Copy variable value] Copying the value of a basic type variable will create a new value on the variable object, and then copy the value to The location where the new variable is assigned. Operations on any variable value do not affect each other.

  • [Copy reference pointer] Copying the value of a reference type variable will also copy the value stored in the variable object into the space allocated by the new variable. The difference is this The copy of the value is actually a pointer to an object stored in the heap. After copying, both variables will actually refer to the same object. Therefore, changing any variable will affect another variable.

Parameter passing

The parameters of all functions in ECMAScript are passed by value. There are two ways to access variables: by value and by reference, while parameters can only be passed by value.
  Basic type parameter passing: What is passed to the function is a copy of the value, and its modification in the function is not visible to the outside.

var a = 1;
var b = 2;
function change(a, b) {
    var c = a;
    a = b;
    b = c;
    console.log(a);    //2
    console.log(b);    //1
}
change(a, b);
console.log(a);    //1
console.log(b);    //2
Copy after login
  • Reference type parameter passing: What is passed to the function is a reference to the value. The modification of its properties in the function is visible externally, but overwriting it with a new reference is externally visible. Invisible

var a = [1, 2, 3];
var b = [5, 6];
function change(a,b) {
  a[0] = 4;    //对其属性的修改外部可见 
  var c = a;
  a = b;      //用新引用覆盖
  b = c;
  console.log(a);  //"5,6"        
  console.log(b);  //"4,2,3"
}
change(a,b);
console.log(a);    //"4,2,3"
console.log(b);    //"5,6"
Copy after login

  a, b are variables in the change function. When calling the function, the references of a and b are assigned to these two variables, but they cannot change the global of a,b. Because overwriting with a new reference is not visible to the outside, because the function only gets the reference and has no power to change the reference.

var a = [1, 2, 3];
var b = [5, 6];
function change() {
  var c = a;
  a[0] = 4;    //对其属性的修改外部可见 
  a = b;      //用新引用覆盖
  b = c;
}
change(a,b);
console.log(a);  //"5,6" 
console.log(b);  //"4,2,3"
Copy after login

Because js does not have a block-level scope, it cannot find variable a in change, and b will consciously go to the upper layer to find it, so a and b here are references to global variables.

The above is the detailed content of Js parameter passing and variable copying. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template