Table of Contents
Copy variable value
Parameter passing
Home Web Front-end JS Tutorial Js parameter passing and variable copying

Js parameter passing and variable copying

Oct 26, 2017 am 10:05 AM
javascript variable copy

  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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to copy lyrics from QQ Music How to copy lyrics How to copy lyrics from QQ Music How to copy lyrics Mar 12, 2024 pm 08:22 PM

We users should be able to understand the diversity of some functions when using this platform. We know that the lyrics of some songs are very well written. Sometimes we even listen to it several times and feel that the meaning is very profound. So if we want to understand the meaning of it, we want to copy it directly and use it as copywriting. However, if we want to use it, we still need to You just need to learn how to copy lyrics. I believe that everyone is familiar with these operations, but it is indeed a bit difficult to operate on a mobile phone. So in order to give you a better understanding, today the editor is here to help you. A good explanation of some of the above operating experiences. If you also like it, come and take a look with the editor. Don’t miss it.​

PS copy layer shortcut key PS copy layer shortcut key Feb 23, 2024 pm 02:34 PM

In the PS copy layer shortcut keys, we can know that if you want to copy a layer when using PS, you can use the shortcut key [Ctrl+J] for quick copying. This introduction to the shortcut keys for copying layers can tell you the specific operation method. The following is the detailed content, so take a look. PS copy layer shortcut key answer: [Ctrl+J] Specific method: 1. Open the image in PS and select the layer that needs to be copied. 2. Press [Ctrl+J] on the keyboard at the same time to complete the copy of the layer. Other copying methods: 1. After opening the image, press and hold the layer and move the [New Layer] icon downwards. 2. After moving to the icon, let go. 3. The layer copy is completed.

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

What should I add to the copy shortcut key ctrl? What should I add to the copy shortcut key ctrl? Mar 15, 2024 am 09:57 AM

On Windows, the shortcut key for copying is Ctrl C; on Apple, the shortcut key for copying is Command C; on Linux, the shortcut key for copying is Ctrl Shift C. Knowing these shortcut keys can improve the user's work efficiency and facilitate text or file copy operations.

See all articles