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 = 101
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 = 103
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 = 105
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 = 106
function
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 = 108
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 = 109
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 = 106
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 = 106
function
0Before 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 = 109
function
2
function
4
function
6
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 = 109
function
8
function
0
Passbyvalue[a, b] {
1
Passbyvalue[a, b] {
2
function
0
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 = 106
function
4Before 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 = 106
2Before 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 = 106
function
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 = 108
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 = 109
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 = 106
let 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 = 109
function
2
function
4
function
6
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 = 109
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 = 106
4let 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 = 106
4let 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 = 106
let 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 = 109
function
2
function
4
function
6
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 = 109
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 = 1010
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 = 1011
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 = 106
4let 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 = 106
4let 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"}