Tóm lược. trong hướng dẫn này, bạn sẽ tìm hiểu về phương thức
2 của JavaScript và cách sử dụng nó hiệu quả hơnCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Giới thiệu về phương thức call[] trong JavaScript
Trong JavaScript, một hàm là một thể hiện của loại
3. Ví dụCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Code language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
Loại
4 có phương thứcCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
2 với cú pháp như sauCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
Trong cú pháp này, phương thức
2 gọi một hàmCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
7 với các đối số [Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
8,Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
9,…] vàCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
00 được đặt thành đối tượngCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
01 bên trong hàmCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
01 là đối tượng mà đối tượng
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
00 tham chiếu bên trong hàm
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
7
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
8,
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
9,. là các đối số hàm được truyền vào
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
7
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Phương thức
2 trả về kết quả của việc gọi phương thứcCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
09Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Ví dụ sau định nghĩa hàm
00 và gọi nó bình thườngCode language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Phần sau gọi hàm
00 nhưng thay vào đó sử dụng phương thứcCode language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
2Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
0Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Theo mặc định,
03 bên trong hàm được đặt thành đối tượng toàn cầu i. e. ,Code language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
04 trong trình duyệt web và
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
Code language: JavaScript [javascript]
05 trong Node. jsCode language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
Lưu ý rằng ở chế độ nghiêm ngặt,
00 bên trong hàm được đặt thànhCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
07 thay vì đối tượng chungCode language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
Xem xét ví dụ sau
0Code language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
Bên trong hàm
08, chúng ta tham chiếu đếnCode language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
09 thông qua giá trịCode language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
00. Nếu bạn chỉ gọi hàmCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
08 thông qua phương thứcCode language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
2 như sauCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
6Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Nó sẽ hiển thị đầu ra sau cho bảng điều khiển
7Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Tuy nhiên, khi bạn gọi phương thức
2 của đối tượng hàmCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
64 và chuyển đối tượngCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
65 làm giá trịCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
00Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
2Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
đầu ra sẽ là
3Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Trong trường hợp này, giá trị
00 bên trong hàmCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
08 tham chiếu đối tượngCode language: JavaScript [javascript]
function add[x, y] { return x + y; } console.log[add instanceof Function]; // true
65, không phải đối tượng toàn cầuCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Sử dụng phương thức call[] của JavaScript để xâu chuỗi các hàm tạo cho một đối tượng
Bạn có thể sử dụng phương thức
2 để xâu chuỗi các hàm tạo của một đối tượng. Xem xét ví dụ sauCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
8Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
đầu ra
0Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
trong ví dụ này
- Đầu tiên, khởi tạo đối tượng
71 với hai thuộc tính.
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
72 và
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
73
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
- Thứ hai, gọi phương thức
2 của đối tượng
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
71 bên trong đối tượng
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
76, đặt giá trị
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
00 cho đối tượng
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
76
Code language: JavaScript [javascript]function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Sử dụng phương thức call[] của JavaScript để mượn hàm
Ví dụ sau minh họa cách sử dụng phương thức call[] để mượn các hàm
1Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
đầu ra
2Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
Làm thế nào nó hoạt động
Đầu tiên, định nghĩa một đối tượng xe ô tô với một tên thuộc tính và ba phương thức
79,Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
20 vàCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
21Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
3Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
Thứ hai, xác định đối tượng máy bay bằng một tên thuộc tính và một phương thức
4Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
Thứ ba, gọi phương thức
22 vàCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
23 của đối tượngCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
24 và phương thứcCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
25 của đối tượngCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
26. Tuy nhiên, chuyểnCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
26 làm đối số đầu tiên vào các phương thứcCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
22 vàCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
23Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
5Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
Bên trong các phương thức
22 vàCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
23,Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
00 tham chiếu đối tượngCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
26, không phải đối tượng
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Code language: JavaScript [javascript]
24. Do đó,Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
35 trả về chuỗiCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
36. Do đó, các phương thức xuất thông báo sauCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
6Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
Về mặt kỹ thuật, đối tượng
26 mượn phương thứcCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
22 vàCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
23 của đối tượngCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
24. Và chức năng mượn đề cập đến một đối tượng sử dụng một phương thức của một đối tượng khácCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Ví dụ sau minh họa cách đối tượng
81 mượn phương thứcCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
82 củaCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
83 thông qua hàmCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
2Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
7Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
đầu ra
8Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
Làm thế nào nó hoạt động
Đầu tiên, xác định hàm
85 trả về giá trị true nếu số đó là số lẻCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
9Code language: JavaScript [javascript]
functionName.call[thisArg, arg1, arg2, ...];
Thứ hai, xác định hàm
86 chấp nhận bất kỳ số lượng đối số nào và trả về một mảng chỉ chứa các số lẻCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
0Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Trong ví dụ này, đối tượng
81 mượn phương thức filter[] của đối tượngCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
83Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Thứ ba, gọi hàm
86Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
1Code language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30
Trong hướng dẫn này, bạn đã học về phương thức
2 của JavaScript và cách sử dụng nó hiệu quả hơnCode language: JavaScript [javascript]
function add[x, y] { return x + y; } let result = add[10, 20]; console.log[result]; // 30