Trong Pass by Value, Hàm được gọi bằng cách truyền trực tiếp giá trị của biến làm đối số. Thay đổi đối số bên trong hàm không ảnh hưởng đến biến được truyền từ bên ngoài hàm
Javascript luôn chuyển theo giá trị, vì vậy việc thay đổi giá trị của biến không bao giờ thay đổi nguyên hàm cơ bản [Chuỗi hoặc số]
function callByValue[varOne, varTwo] {
console.log["Inside Call by Value Method"];
varOne = 100;
varTwo = 200;
console.log["varOne =" + varOne +"varTwo =" +varTwo];
} let varOne = 10;
let varTwo = 20; console.log["Before Call by Value Method"];
console.log["varOne =" + varOne +"varTwo =" +varTwo]; callByValue[varOne, varTwo] console.log["After Call by Value Method"];
console.log["varOne =" + varOne +"varTwo =" +varTwo];
output will be :
---------------
Before Call by Value Method
varOne =10 varTwo =20
Inside Call by Value Method
varOne =100 varTwo =200
After Call by Value Method
varOne =10 varTwo =20
Tuy nhiên, khi một biến đề cập đến một đối tượng bao gồm mảng, giá trị là tham chiếu đến đối tượng
Vượt qua tham khảo
Trong Pass by Reference, Hàm được gọi bằng cách chuyể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 theo sau chuyển qua tham chiếu
function callByReference[varObj] {
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
vì vậy nếu chúng ta chuyển đối tượng hoặc mảng làm đối số cho phương thức, thì có khả năng giá trị của đối tượng có thể thay đổi
Các hàm là một trong những khối xây dựng cơ bản trong JavaScript. Một hàm trong JavaScript tương tự như một thủ tục—một tập hợp các câu lệnh thực hiện một tác vụ hoặc tính toán một giá trị, nhưng để một thủ tục đủ điều kiện là một hàm, nó phải nhận một số đầu vào và trả về một đầu ra khi có một số mối quan hệ rõ ràng giữa . Để sử dụng một chức năng, bạn phải xác định nó ở đâu đó trong phạm vi mà bạn muốn gọi nó
Xem thêm chương tham khảo đầy đủ về các hàm JavaScript để biết chi tiết
Một định nghĩa hàm [còn được gọi là khai báo hàm hoặc câu lệnh hàm] bao gồm từ khóa
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
9, theo sau là- Tên của chức năng
- Danh sách các tham số của hàm, được đặt trong dấu ngoặc đơn và được phân tách bằng dấu phẩy
- Các câu lệnh JavaScript xác định hàm, được đặt trong dấu ngoặc nhọn,
0const square = function [number] { return number * number; } const x = square[4]; // x gets the value 16
Ví dụ: đoạn mã sau định nghĩa một hàm đơn giản có tên là
const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
1function square[number] {
return number * number;
}
Hàm
const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
1 nhận một tham số, được gọi là const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
3. Hàm bao gồm một câu lệnh cho biết trả về tham số của hàm [tức là, const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
3] nhân với chính nó. Câu lệnh const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
5 chỉ định giá trị được trả về bởi hàmfunction callByReference[varObj] {0
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Về cơ bản, các tham số được truyền cho các hàm theo giá trị — vì vậy nếu mã trong phần thân của hàm gán một giá trị hoàn toàn mới cho một tham số được truyền cho hàm, thì thay đổi đó không được phản ánh trên toàn cục hoặc trong mã gọi hàm đó
Khi bạn chuyển một đối tượng làm tham số, nếu hàm thay đổi thuộc tính của đối tượng, thì thay đổi đó sẽ hiển thị bên ngoài hàm, như minh họa trong ví dụ sau
function callByReference[varObj] {1
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Khi bạn chuyển một mảng dưới dạng tham số, nếu hàm thay đổi bất kỳ giá trị nào của mảng, thì thay đổi đó sẽ hiển thị bên ngoài hàm, như minh họa trong ví dụ sau
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
Mặc dù khai báo hàm ở trên là một câu lệnh về mặt cú pháp, các hàm cũng có thể được tạo bởi một biểu thức hàm
Một chức năng như vậy có thể ẩn danh; . Ví dụ, hàm
const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
1 có thể được định nghĩa làconst square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
Tuy nhiên, một tên có thể được cung cấp với một biểu thức chức năng. Việc cung cấp tên cho phép hàm tham chiếu đến chính nó và cũng giúp xác định hàm dễ dàng hơn trong dấu vết ngăn xếp của trình gỡ lỗi
function callByReference[varObj] {5
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Các biểu thức hàm thuận tiện khi chuyển một hàm làm đối số cho một hàm khác. Ví dụ sau đây cho thấy một hàm
const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
7 sẽ nhận một hàm làm đối số thứ nhất và một mảng làm đối số thứ haifunction callByReference[varObj] {7
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Trong đoạn mã sau, hàm nhận một hàm được xác định bởi một biểu thức hàm và thực thi nó cho mọi phần tử của mảng nhận được dưới dạng đối số thứ hai
function callByReference[varObj] {8
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hàm trả về.
const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
8Trong JavaScript, một chức năng có thể được xác định dựa trên một điều kiện. Ví dụ: định nghĩa hàm sau chỉ xác định
const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
9 nếu function callByReference[varObj] {50 bằng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {51
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
3Ngoài việc xác định các hàm như được mô tả ở đây, bạn cũng có thể sử dụng hàm tạo
function callByReference[varObj] {52 để tạo các hàm từ một chuỗi trong thời gian chạy, giống như
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {53
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Một phương thức là một chức năng là một thuộc tính của một đối tượng. Đọc thêm về đối tượng và phương pháp trong Làm việc với đối tượng
Xác định một chức năng không thực thi nó. Xác định nó đặt tên cho hàm và chỉ định những việc cần làm khi hàm được gọi
Gọi hàm thực sự thực hiện các hành động được chỉ định với các tham số được chỉ định. Ví dụ, nếu bạn định nghĩa hàm
const square = function [number] {
return number * number;
}
const x = square[4]; // x gets the value 16
1, bạn có thể gọi nó như saufunction myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
7Câu lệnh trước gọi hàm với đối số là
function callByReference[varObj] {55. Hàm thực thi các câu lệnh của nó và trả về giá trị
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {56
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hàm phải nằm trong phạm vi khi chúng được gọi, nhưng phần khai báo hàm có thể nằm trong phạm vi [xuất hiện bên dưới lệnh gọi trong mã]. Phạm vi của một khai báo hàm là hàm mà nó được khai báo [hoặc toàn bộ chương trình, nếu nó được khai báo ở mức cao nhất]
Các đối số của hàm không giới hạn ở chuỗi và số. Bạn có thể chuyển toàn bộ đối tượng vào một hàm. Hàm
function callByReference[varObj] {57 [được định nghĩa trong ] là một ví dụ về hàm lấy một đối tượng làm đối số
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Một chức năng có thể gọi chính nó. Ví dụ, đây là một hàm tính giai thừa theo cách đệ quy
function callByReference[varObj] {00
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Sau đó, bạn có thể tính giai thừa của
function callByReference[varObj] {58 đến
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {55 như sau
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {01
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Có nhiều cách khác để gọi hàm. Thường có những trường hợp một hàm cần được gọi động hoặc số lượng đối số cho một hàm khác nhau hoặc trong đó ngữ cảnh của lệnh gọi hàm cần được đặt thành một đối tượng cụ thể được xác định trong thời gian chạy
Hóa ra bản thân các hàm là các đối tượng - và ngược lại, các đối tượng này có các phương thức. [Xem đối tượng
function callByReference[varObj] {52. ] Các phương pháp
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {71 và
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {72 có thể được sử dụng để đạt được mục tiêu này
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hãy xem xét ví dụ dưới đây
function callByReference[varObj] {02
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Đoạn mã này chạy không có bất kỳ lỗi nào, mặc dù hàm
function callByReference[varObj] {73 được gọi trước khi nó được khai báo. Điều này là do trình thông dịch JavaScript nâng toàn bộ khai báo hàm lên đầu phạm vi hiện tại, vì vậy đoạn mã trên tương đương với
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {03
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Tính năng nâng hàm chỉ hoạt động với các khai báo hàm — không hoạt động với các biểu thức hàm. Mã bên dưới sẽ không hoạt động
function callByReference[varObj] {04
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Các biến được xác định bên trong một hàm không thể được truy cập từ bất kỳ đâu bên ngoài hàm, bởi vì biến chỉ được xác định trong phạm vi của hàm. Tuy nhiên, một hàm có thể truy cập tất cả các biến và hàm được xác định bên trong phạm vi mà nó được xác định
Nói cách khác, một hàm được xác định trong phạm vi toàn cầu có thể truy cập tất cả các biến được xác định trong phạm vi toàn cầu. Một hàm được xác định bên trong một hàm khác cũng có thể truy cập tất cả các biến được xác định trong hàm cha của nó và bất kỳ biến nào khác mà hàm cha có quyền truy cập
function callByReference[varObj] {05
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Một chức năng có thể tham khảo và gọi chính nó. Có ba cách để một hàm tham chiếu đến chính nó
- Tên chức năng
function callByReference[varObj] {
74
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}- Một biến trong phạm vi đề cập đến chức năng
Ví dụ, xét định nghĩa hàm sau
function callByReference[varObj] {06
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Trong thân hàm, tất cả những điều sau đây đều tương đương
function callByReference[varObj] {
75
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
76
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
77
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hàm gọi chính nó được gọi là hàm đệ quy. Trong một số cách, đệ quy tương tự như một vòng lặp. Cả hai đều thực thi cùng một mã nhiều lần và cả hai đều yêu cầu một điều kiện [để tránh vòng lặp vô hạn, hay đúng hơn là đệ quy vô hạn trong trường hợp này]
Ví dụ, xét vòng lặp sau
function callByReference[varObj] {07
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Nó có thể được chuyển đổi thành một khai báo hàm đệ quy, theo sau là lời gọi hàm đó
function callByReference[varObj] {08
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Tuy nhiên, một số thuật toán không thể là các vòng lặp đơn giản. Ví dụ: lấy tất cả các nút của cấu trúc cây [chẳng hạn như DOM] dễ dàng hơn thông qua đệ quy
function callByReference[varObj] {09
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
So với hàm
function callByReference[varObj] {78, mỗi lệnh gọi đệ quy tự nó thực hiện nhiều lệnh gọi đệ quy ở đây
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Có thể chuyển đổi bất kỳ thuật toán đệ quy nào thành thuật toán không đệ quy, nhưng logic thường phức tạp hơn nhiều và làm như vậy yêu cầu sử dụng ngăn xếp
Trên thực tế, bản thân đệ quy sử dụng ngăn xếp. ngăn xếp chức năng. Hành vi giống như ngăn xếp có thể được nhìn thấy trong ví dụ sau
function callByReference[varObj] {10
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Bạn có thể lồng một chức năng trong một chức năng khác. Hàm lồng nhau [bên trong] là riêng tư đối với hàm chứa [bên ngoài] của nó
Nó cũng tạo thành một đóng cửa. Bao đóng là một biểu thức [phổ biến nhất là một hàm] có thể có các biến tự do cùng với một môi trường liên kết các biến đó ["đóng" biểu thức]
Vì một hàm lồng nhau là một hàm bao đóng, điều này có nghĩa là một hàm lồng nhau có thể "kế thừa" các đối số và biến của hàm chứa nó. Nói cách khác, hàm bên trong chứa phạm vi của hàm bên ngoài
Để tóm tắt
- Hàm bên trong chỉ có thể được truy cập từ các câu lệnh trong hàm bên ngoài
- Hàm bên trong tạo thành một bao đóng. hàm bên trong có thể sử dụng các đối số và biến của hàm bên ngoài, trong khi hàm bên ngoài không thể sử dụng các đối số và biến của hàm bên trong
Ví dụ sau đây cho thấy các hàm lồng nhau
function callByReference[varObj] {11
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Vì hàm bên trong tạo thành một bao đóng, nên bạn có thể gọi hàm bên ngoài và chỉ định các đối số cho cả hàm bên ngoài và bên trong
function callByReference[varObj] {12
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Lưu ý cách
function callByReference[varObj] {79 được bảo toàn khi trả về
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {80. Một bao đóng phải bảo toàn các đối số và biến trong tất cả các phạm vi mà nó tham chiếu. Vì mỗi lệnh gọi cung cấp các đối số có khả năng khác nhau nên một lần đóng mới được tạo cho mỗi lệnh gọi tới
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {81. Bộ nhớ chỉ có thể được giải phóng khi
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {80 được trả về không còn truy cập được nữa
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Điều này không khác với việc lưu trữ các tham chiếu trong các đối tượng khác, nhưng thường ít rõ ràng hơn vì người ta không đặt trực tiếp các tham chiếu và không thể kiểm tra chúng
Các chức năng có thể được nhân lồng nhau. Ví dụ
- Hàm [
function callByReference[varObj] {
83] chứa hàm [
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
84], chính hàm này chứa hàm [
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
85]
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100} - Cả hai hàm
function callByReference[varObj] {
84 và
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
85 đóng biểu mẫu ở đây. Vì vậy,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
84 có thể truy cập ________ 183 và ________ 185 có thể truy cập ________ 184
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100} - Ngoài ra, vì
function callByReference[varObj] {
85 có thể truy cập
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
84 có thể truy cập
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
83, nên
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
85 cũng có thể truy cập
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
83
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Do đó, các bao đóng có thể chứa nhiều phạm vi; . Điều này được gọi là chuỗi phạm vi. [Lý do nó được gọi là "chuỗi" sẽ được giải thích sau. ]
Xem xét ví dụ sau
function callByReference[varObj] {13
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Trong ví dụ này,
function callByReference[varObj] {85 truy cập
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
39 của function callByReference[varObj] {84 và
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {79 của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {83
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Điều này có thể được thực hiện bởi vì
function callByReference[varObj] {
84 tạo thành một bao đóng bao gồm
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
83 [i. e. ,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
84 có thể truy cập các đối số và biến của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
83]
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
85 tạo thành một bao đóng bao gồm
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
84
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}- Bởi vì bao đóng của
function callByReference[varObj] {
85 bao gồm cả
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
84 và bao đóng của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
84 bao gồm cả
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
83, nên bao đóng của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
85 cũng bao gồm cả
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
83. Điều này có nghĩa là
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
85 có thể truy cập cả đối số và biến của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
84 và
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
83. Nói cách khác,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
85 xâu chuỗi phạm vi của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
84 và
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}function callByReference[varObj] {
83, theo thứ tự đó
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Tuy nhiên, điều ngược lại là không đúng.
function callByReference[varObj] {83 không thể truy cập
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {85, vì
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {83 không thể truy cập bất kỳ đối số hoặc biến nào của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {84, mà
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {85 là một biến của. Do đó,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {85 vẫn là riêng tư đối với chỉ
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {84
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Khi hai đối số hoặc biến trong phạm vi đóng có cùng tên, sẽ xảy ra xung đột tên. Nhiều phạm vi lồng nhau hơn được ưu tiên. Vì vậy, phạm vi trong cùng có mức độ ưu tiên cao nhất, trong khi phạm vi ngoài cùng có mức độ ưu tiên thấp nhất. Đây là chuỗi phạm vi. Đầu tiên trên chuỗi là phạm vi trong cùng và cuối cùng là phạm vi ngoài cùng. Hãy xem xét những điều sau đây
function callByReference[varObj] {14
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Xung đột tên xảy ra tại câu lệnh
function callByReference[varObj] {017 và nằm giữa tham số của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {80 là
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {79 và biến của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {81 là
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {79. Chuỗi phạm vi ở đây là {
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {80,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {81, đối tượng toàn cục}. Do đó,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {79 của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {80 được ưu tiên hơn
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {79 của
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {81 và
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {028 [_______179 của ____180] được trả về thay vì ____ 1031 [_______ 181 của _______ 179]
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Bao đóng là một trong những tính năng mạnh mẽ nhất của JavaScript. JavaScript cho phép lồng các hàm và cấp cho hàm bên trong toàn quyền truy cập vào tất cả các biến và hàm được xác định bên trong hàm bên ngoài [và tất cả các biến và hàm khác mà hàm bên ngoài có quyền truy cập]
Tuy nhiên, hàm bên ngoài không có quyền truy cập vào các biến và hàm được xác định bên trong hàm bên trong. Điều này cung cấp một loại đóng gói cho các biến của hàm bên trong
Ngoài ra, vì chức năng bên trong có quyền truy cập vào phạm vi của chức năng bên ngoài, các biến và hàm được xác định trong chức năng bên ngoài sẽ tồn tại lâu hơn thời lượng thực thi chức năng bên ngoài, nếu chức năng bên trong quản lý để tồn tại ngoài vòng đời của chức năng bên ngoài. . Một bao đóng được tạo khi chức năng bên trong bằng cách nào đó được cung cấp cho bất kỳ phạm vi nào bên ngoài chức năng bên ngoài
function callByReference[varObj] {15
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Nó có thể phức tạp hơn nhiều so với đoạn mã trên. Một đối tượng chứa các phương thức để thao tác các biến bên trong của hàm bên ngoài có thể được trả về
function callByReference[varObj] {16
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Trong đoạn mã trên, biến
function callByReference[varObj] {034 của hàm bên ngoài có thể truy cập được đối với các hàm bên trong và không có cách nào khác để truy cập các biến bên trong ngoại trừ thông qua các hàm bên trong. Các biến bên trong của các hàm bên trong hoạt động như các kho lưu trữ an toàn cho các đối số và biến bên ngoài. Chúng giữ dữ liệu "liên tục" và "đóng gói" để các chức năng bên trong hoạt động với. Các chức năng thậm chí không phải được gán cho một biến hoặc có tên
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {17
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Ghi chú. Có một số cạm bẫy cần chú ý khi sử dụng bao đóng
Nếu một hàm kèm theo định nghĩa một biến có cùng tên với một biến trong phạm vi bên ngoài, thì không có cách nào để tham chiếu lại biến đó trong phạm vi bên ngoài. [Biến phạm vi bên trong "đè" biến bên ngoài, cho đến khi chương trình thoát khỏi phạm vi bên trong. Nó có thể được coi là một. ]
function callByReference[varObj] {18
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Các đối số của một hàm được duy trì trong một đối tượng giống như mảng. Trong một hàm, bạn có thể giải quyết các đối số được truyền cho nó như sau
function callByReference[varObj] {19
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
trong đó
function callByReference[varObj] {035 là số thứ tự của đối số, bắt đầu từ
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {51. Vì vậy, đối số đầu tiên được truyền cho một hàm sẽ là
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {037. Tổng số đối số được biểu thị bằng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {038
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Sử dụng đối tượng
function callByReference[varObj] {039, bạn có thể gọi một hàm có nhiều đối số hơn nó được khai báo chính thức để chấp nhận. Điều này thường hữu ích nếu bạn không biết trước có bao nhiêu đối số sẽ được truyền cho hàm. Bạn có thể sử dụng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {038 để xác định số lượng đối số thực sự được truyền cho hàm, sau đó truy cập từng đối số bằng cách sử dụng đối tượng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {039
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Ví dụ, xét một hàm nối nhiều chuỗi. Đối số chính thức duy nhất cho hàm là một chuỗi chỉ định các ký tự phân tách các mục để nối. Hàm được định nghĩa như sau
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
0Bạn có thể chuyển bất kỳ số lượng đối số nào cho hàm này và nó nối từng đối số thành một chuỗi "danh sách"
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
1Ghi chú. Biến
function callByReference[varObj] {039 là "giống như mảng", nhưng không phải là một mảng. Nó giống mảng ở chỗ nó có chỉ mục được đánh số và thuộc tính
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {043. Tuy nhiên, nó không sở hữu tất cả các phương thức thao tác mảng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Xem đối tượng
function callByReference[varObj] {52 trong tài liệu tham khảo JavaScript để biết thêm thông tin
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Có hai loại cú pháp tham số đặc biệt. tham số mặc định và tham số còn lại
Trong JavaScript, tham số của hàm mặc định là
function callByReference[varObj] {045. Tuy nhiên, trong một số trường hợp, có thể hữu ích khi đặt một giá trị mặc định khác. Đây chính xác là những gì các thông số mặc định làm
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Trước đây, chiến lược chung để đặt giá trị mặc định là kiểm tra các giá trị tham số trong phần thân của hàm và gán giá trị nếu chúng là
function callByReference[varObj] {045
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Trong ví dụ sau, nếu không có giá trị nào được cung cấp cho
function callByReference[varObj] {047, thì giá trị của nó sẽ là
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {045 khi đánh giá
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {049 và lệnh gọi tới
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {050 thường sẽ trả về
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {051. Tuy nhiên, điều này bị ngăn chặn bởi dòng thứ hai trong ví dụ này
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
2Với các tham số mặc định, việc kiểm tra thủ công trong thân hàm không còn cần thiết nữa. Bạn có thể đặt
function callByReference[varObj] {58 làm giá trị mặc định cho
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {047 trong phần đầu của hàm
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
3Để biết thêm chi tiết, hãy xem các tham số mặc định trong tài liệu tham khảo
Cú pháp tham số còn lại cho phép chúng ta biểu diễn một số lượng đối số không xác định dưới dạng một mảng
Trong ví dụ sau, hàm
function callByReference[varObj] {050 sử dụng các tham số còn lại để thu thập các đối số từ phần thứ hai đến phần cuối. Sau đó, hàm sẽ nhân chúng với đối số đầu tiên
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
4Biểu thức hàm mũi tên [còn được gọi là mũi tên béo để phân biệt với cú pháp
function callByReference[varObj] {055 giả định trong JavaScript tương lai] có cú pháp ngắn hơn so với biểu thức hàm và không có
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {056,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {039,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {058 hoặc
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {059 riêng. Các chức năng mũi tên luôn ẩn danh
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hai yếu tố ảnh hưởng đến việc giới thiệu các chức năng mũi tên. chức năng ngắn hơn và không ràng buộc của
function callByReference[varObj] {056
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Trong một số mẫu chức năng, các chức năng ngắn hơn được hoan nghênh. So sánh
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
5Cho đến các hàm mũi tên, mọi hàm mới đều xác định giá trị
function callByReference[varObj] {056 của riêng nó [một đối tượng mới trong trường hợp hàm tạo, không xác định trong các lệnh gọi hàm chế độ nghiêm ngặt, đối tượng cơ sở nếu hàm được gọi là "phương thức đối tượng", v.v. ]. Điều này được chứng minh là không lý tưởng với phong cách lập trình hướng đối tượng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
6Trong ECMAScript 3/5, sự cố này đã được khắc phục bằng cách gán giá trị trong
function callByReference[varObj] {056 cho một biến có thể được đóng lại
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
7Ngoài ra, một chức năng liên kết có thể được tạo để giá trị
function callByReference[varObj] {056 thích hợp sẽ được chuyển đến chức năng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {064
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hàm mũi tên không có
function callByReference[varObj] {056 của riêng nó; . Do đó, trong đoạn mã sau,
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {056 trong hàm được truyền cho
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {068 có cùng giá trị với
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {056 trong hàm kèm theo
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function myFunc[theArr] {
theArr[0] = 30;
}
const arr = [45];
console.log[arr[0]]; // 45
myFunc[arr];
console.log[arr[0]]; // 30
8JavaScript có một số chức năng tích hợp, cấp cao nhất
function callByReference[varObj] {53
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Phương thức
function callByReference[varObj] {53 đánh giá mã JavaScript được biểu diễn dưới dạng chuỗi
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {072
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hàm toàn cầu
function callByReference[varObj] {072 xác định xem giá trị được truyền có phải là một số hữu hạn hay không. Nếu cần, tham số đầu tiên được chuyển đổi thành một số
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {074
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hàm
function callByReference[varObj] {074 xác định xem một giá trị có phải là
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {051 hay không. Ghi chú. cưỡng chế bên trong hàm
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {077 có quy tắc;
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {079
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hàm
function callByReference[varObj] {079 phân tích cú pháp một đối số chuỗi và trả về một số dấu phẩy động
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {081
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hàm
function callByReference[varObj] {081 phân tích cú pháp một đối số chuỗi và trả về một số nguyên của cơ số đã chỉ định [cơ số trong các hệ thống số toán học]
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {083
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Hàm
function callByReference[varObj] {083 giải mã Mã định danh tài nguyên đồng nhất [URI] được tạo trước đó bởi
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {085 hoặc bởi một quy trình tương tự
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {086
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Phương thức
function callByReference[varObj] {086 giải mã thành phần Mã định danh tài nguyên đồng nhất [URI] được tạo trước đó bởi
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {088 hoặc bởi một quy trình tương tự
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {089
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Phương thức
function callByReference[varObj] {089 mã hóa Mã định danh tài nguyên đồng nhất [URI] bằng cách thay thế từng phiên bản của các ký tự nhất định bằng một, hai, ba hoặc bốn chuỗi thoát đại diện cho mã hóa UTF-8 của ký tự [sẽ chỉ có bốn chuỗi thoát cho các ký tự bao gồm hai
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {091
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Phương thức
function callByReference[varObj] {091 mã hóa thành phần Mã định danh tài nguyên đồng nhất [URI] bằng cách thay thế từng phiên bản của các ký tự nhất định bằng một, hai, ba hoặc bốn chuỗi thoát đại diện cho mã hóa UTF-8 của ký tự [sẽ chỉ có bốn chuỗi thoát cho các ký tự bao gồm
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {093
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Phương thức
function callByReference[varObj] {093 không dùng nữa tính toán một chuỗi mới trong đó một số ký tự nhất định đã được thay thế bằng một chuỗi thoát thập lục phân. Sử dụng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {085 hoặc
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {088 để thay thế
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {097
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
Phương thức
function callByReference[varObj] {097 không dùng nữa tính toán một chuỗi mới trong đó các chuỗi thoát thập lục phân được thay thế bằng ký tự mà nó đại diện. Các chuỗi thoát có thể được giới thiệu bởi một chức năng như
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {099. Bởi vì
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {097 không được dùng nữa, hãy sử dụng
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {083 hoặc
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}
function callByReference[varObj] {102 để thay thế
console.log["Inside Call by Reference Method"];
varObj.a = 100;
console.log[varObj];
}
let varObj = {a:1};console.log["Before Call by Reference Method"];
console.log[varObj];callByReference[varObj] console.log["After Call by Reference Method"];
console.log[varObj];output will be :
--------------- Before Call by Reference Method
{a: 1}
Inside Call by Reference Method
{a: 100}
After Call by Reference Method
{a: 100}