Hướng dẫn what is pass by value and pass by reference in javascript? - chuyển theo giá trị và chuyển theo tham chiếu trong javascript là gì?

Trong bài viết này, chúng tôi sẽ nói về việc vượt qua giá trị và vượt qua tham chiếu trong JavaScript. & nbsp;

Vượt qua giá trị: Trong Pass by giá trị, hàm được gọi bằng cách truyền trực tiếp giá trị của biến dưới dạng đối số. Vì vậy, bất kỳ thay đổi nào được thực hiện bên trong hàm không ảnh hưởng đến giá trị ban đầu. In Pass by value, function is called by directly passing the value of the variable as an argument. So any changes made inside the function does not affect the original value.

Trong Pass by giá trị, các tham số được truyền dưới dạng một đối số tạo bản sao của riêng nó. Vì vậy, bất kỳ thay đổi nào được thực hiện bên trong hàm được thực hiện đối với giá trị sao chép không theo giá trị ban đầu.own copy. So any changes made inside the function is made to the copied value not to the original value .

Hãy để chúng tôi lấy một ví dụ để hiểu rõ hơn:

JavaScript

function Passbyvalue[a, b] {

    let tmp;

    tmp = b;

    

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
1

    

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
3

    

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
5

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6function
Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
8

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
9

Updating the object reference -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20}
0

Updating the object reference -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20}
1

Updating the object reference -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20}
2

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6
Updating the object reference -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20}
4

Updating the object reference -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20}
5

Updating the object reference -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20}
6

Updating the object reference -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20}
7
Updating the object reference -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20}
8

Output:

Before calling Pass by value Function -> a = 1 b = 2
Inside Pass by value function -> a = 2 b = 1
After calling Pass by value Function -> a =1 b = 2

Vượt qua tham chiếu: Trong tham chiếu vượt qua, hàm được gọi bằng cách truyền trực tiếp tham chiếu/địa chỉ của biến làm đối số. Vì vậy, thay đổi giá trị bên trong hàm cũng thay đổi giá trị ban đầu. Trong mảng JavaScript và đối tượng theo sau bởi thuộc tính tham chiếu. In Pass by Reference, Function is called by directly passing the reference/address of the variable as an argument. So changing the value inside the function also change the original value. In JavaScript array and Object follows pass by reference property.

Trong tham chiếu vượt qua, các tham số được truyền dưới dạng đối số không tạo bản sao của riêng mình, nó đề cập đến giá trị ban đầu để các thay đổi được thực hiện bên trong ảnh hưởng đến giá trị ban đầu. & NBSP;

Hãy để chúng tôi lấy một ví dụ để hiểu rõ hơn.

JavaScript

function Passbyvalue[a, b] {

    

Mutating the origanal object -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
2

    

Mutating the origanal object -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
4

    

Mutating the origanal object -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
6

    

Mutating the origanal object -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
8

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6function0

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
9

function2

    function4

    function6

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
9

function8

    function0

Passbyvalue[a, b] {1

Passbyvalue[a, b] {2

    function0

Output:

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6function
Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
8
In Pass by Reference, we are mutating the original value. when we pass an object as an arguments and update that object’s reference in the function’s context, that won’t affect the object value. But if we mutate the object internally, It will affect the object .

Vượt qua tham chiếu: Trong tham chiếu vượt qua, hàm được gọi bằng cách truyền trực tiếp tham chiếu/địa chỉ của biến làm đối số. Vì vậy, thay đổi giá trị bên trong hàm cũng thay đổi giá trị ban đầu. Trong mảng JavaScript và đối tượng theo sau bởi thuộc tính tham chiếu. Updating the object reference in the function.

JavaScript

function Passbyvalue[a, b] {

    Passbyvalue[a, b] {8

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6function4

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6    2

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6function
Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
8

    

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
9

        9

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6let tmp;1

    let tmp;3

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
9

function2

    function4

    function6

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
9

    1

    2

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6    4

let tmp;3

Passbyvalue[a, b] {1

    7

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6    4

let tmp;3

Output:

Updating the object reference -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20}

Vượt qua tham chiếu: Trong tham chiếu vượt qua, hàm được gọi bằng cách truyền trực tiếp tham chiếu/địa chỉ của biến làm đối số. Vì vậy, thay đổi giá trị bên trong hàm cũng thay đổi giá trị ban đầu. Trong mảng JavaScript và đối tượng theo sau bởi thuộc tính tham chiếu. Mutating the original Object.

JavaScript

function Passbyvalue[a, b] {

    tmp = b;4    5tmp = b;6

    tmp = b;8

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6let tmp;1

    let tmp;3

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
9

function2

    function4

    function6

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
9

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
10

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
11

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6    4

let tmp;3

Passbyvalue[a, b] {1

    7

Before calling Pass By Reference Function -> a = 10 b = 20
Inside Pass By Reference Function -> a = 20 b = 10
After calling Pass By Reference Function -> a = 20 b = 10
6    4

let tmp;3

Output:

Mutating the origanal object -> 
Before calling PassByReference Function -> obj
{a: 10, b: 20}
Inside PassbyReference Function -> obj 
{a: 10, b: 20, c: "GEEKSFORGEEKS"}
After calling PassByReference Function -> obj
{a: 10, b: 20, c: "GEEKSFORGEEKS"}

Những gì vượt qua bằng cách tham khảo trong JS?

Trong tham chiếu vượt qua, một hàm được gọi bằng cách truyền trực tiếp tham chiếu/địa chỉ của biến làm đối số. Thay đổi đối số bên trong hàm ảnh hưởng đến biến được truyền từ bên ngoài hàm. Trong các đối tượng và mảng JavaScript được truyền qua tham chiếu.a function is called by directly passing the reference/address of the variable as the argument. Changing the argument inside the function affects the variable passed from outside the function. In Javascript objects and arrays are passed by reference.

Điều gì vượt qua giá trị có nghĩa là gì trong JavaScript?

Vượt qua giá trị: Trong Pass by giá trị, hàm được gọi bằng cách truyền trực tiếp giá trị của biến dưới dạng đối số.Vì vậy, bất kỳ thay đổi nào được thực hiện bên trong hàm không ảnh hưởng đến giá trị ban đầu.Trong Pass by giá trị, các tham số được truyền dưới dạng một đối số tạo bản sao của riêng nó.function is called by directly passing the value of the variable as an argument. So any changes made inside the function does not affect the original value. In Pass by value, parameters passed as an arguments create its own copy.

JavaScript là tham chiếu hay theo giá trị?

Trong JavaScript, tất cả các đối số hàm luôn được truyền bởi giá trị.Nó có nghĩa là JavaScript sao chép các giá trị của các biến vào các đối số hàm.Bất kỳ thay đổi nào bạn thực hiện đối với các đối số bên trong hàm không phản ánh các biến vượt qua bên ngoài hàm.

Những gì được truyền theo giá trị và vượt qua tham chiếu trong Java với ví dụ?

Truyền theo giá trị: Đó là một quá trình trong đó các giá trị tham số hàm được sao chép sang một biến khác và thay vào đó đối tượng này được sao chép được truyền.Điều này được gọi là cuộc gọi theo giá trị.Vượt qua tham chiếu: Đó là một quá trình trong đó bản sao thực tế của tham chiếu được chuyển đến hàm.Điều này được gọi bằng cách tham khảo.It is a process in which the function parameter values are copied to another variable and instead this object copied is passed. This is known as call by Value. Pass by Reference: It is a process in which the actual copy of reference is passed to the function. This is called by Reference.

Bài Viết Liên Quan

Chủ Đề