Cuộc gọi áp dụng và liên kết trong javascript là gì?

Phương thức

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
4 tạo một hàm mới, khi được gọi, từ khóa
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 của nó được đặt thành giá trị được cung cấp, với một chuỗi đối số đã cho trước bất kỳ hàm nào được cung cấp khi hàm mới được gọi

Thử nó

cú pháp

bind[thisArg]
bind[thisArg, arg1]
bind[thisArg, arg1, arg2]
bind[thisArg, arg1, arg2, /* …, */ argN]

Thông số

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
6

Giá trị được truyền dưới dạng tham số

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 cho hàm đích
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
8 khi hàm liên kết được gọi. Nếu chức năng không ở chế độ nghiêm ngặt,
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
9 và
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
0 sẽ được thay thế bằng đối tượng toàn cục và các giá trị nguyên thủy sẽ được chuyển đổi thành đối tượng. Giá trị bị bỏ qua nếu hàm liên kết được xây dựng bằng toán tử
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
1

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
2 Tùy chọn

Các đối số để thêm vào các đối số được cung cấp cho hàm bị ràng buộc khi gọi

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
8

Giá trị trả về

Một bản sao của hàm đã cho với giá trị

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 được chỉ định và các đối số ban đầu [nếu được cung cấp]

Sự mô tả

Hàm

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
4 tạo một hàm liên kết mới. Việc gọi hàm bị ràng buộc thường dẫn đến việc thực thi hàm mà nó bao hàm, hàm này còn được gọi là hàm đích. Hàm bị ràng buộc sẽ lưu trữ các tham số được truyền — bao gồm giá trị của
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 và một vài đối số đầu tiên — dưới dạng trạng thái bên trong của nó. Các giá trị này được lưu trữ trước, thay vì được chuyển vào thời điểm gọi. Nhìn chung, bạn có thể thấy
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
7 tương đương với
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
8 về hiệu ứng khi nó được gọi [nhưng không phải khi
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
9 được xây dựng]

Một chức năng bị ràng buộc có thể bị ràng buộc hơn nữa bằng cách gọi

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
70, điều này tạo ra một chức năng bị ràng buộc khác là
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
71. Giá trị
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
6 mới bị ràng buộc bị bỏ qua, vì hàm mục tiêu của
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
71, là
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
9, đã có một giá trị ràng buộc
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5. Khi
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
71 được gọi, nó sẽ gọi
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
9, đến lượt nó gọi
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
78. Các đối số mà
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
78 cuối cùng nhận được là, theo thứ tự. các đối số bị ràng buộc bởi
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
9, các đối số bị ràng buộc bởi
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
71 và các đối số nhận được bởi
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
71

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6

Một chức năng bị ràng buộc cũng có thể được xây dựng bằng cách sử dụng toán tử

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
1 nếu chức năng đích của nó có thể xây dựng được. Làm như vậy hoạt động như thể chức năng mục tiêu thay vào đó đã được xây dựng. Các đối số được thêm vào trước được cung cấp cho hàm mục tiêu như bình thường, trong khi giá trị
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 được cung cấp bị bỏ qua [vì quá trình xây dựng chuẩn bị cho hàm đích của nó, như được thấy bởi các tham số của
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
46]. Nếu chức năng ràng buộc được xây dựng trực tiếp, thay vào đó,
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
47 sẽ là chức năng mục tiêu. [Nghĩa là, hàm ràng buộc trong suốt đối với
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
47. ]

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]

Tuy nhiên, vì một hàm bị ràng buộc không có thuộc tính

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
49 nên nó không thể được sử dụng làm lớp cơ sở cho
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
90

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
7

Khi sử dụng một hàm liên kết làm phía bên tay phải của

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
91,
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
91 sẽ tiếp cận hàm đích [được lưu trữ bên trong hàm liên kết] và thay vào đó đọc
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
49 của nó

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
4

Hàm liên kết có các thuộc tính sau

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
94

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
94 của hàm mục tiêu trừ đi số đối số bị ràng buộc [không tính tham số
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
6], với 0 là giá trị nhỏ nhất

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
97

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
97 của hàm mục tiêu cộng với tiền tố
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
99

Hàm liên kết cũng kế thừa chuỗi nguyên mẫu của hàm đích. Tuy nhiên, nó không có các thuộc tính riêng khác của hàm đích [chẳng hạn như các thuộc tính tĩnh nếu hàm đích là một lớp]

ví dụ

Tạo một chức năng ràng buộc

Cách sử dụng đơn giản nhất của

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
4 là tạo một hàm, bất kể nó được gọi như thế nào, được gọi với một giá trị
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 cụ thể

Một lỗi phổ biến đối với các lập trình viên JavaScript mới là trích xuất một phương thức từ một đối tượng, sau đó gọi hàm đó và mong đợi nó sử dụng đối tượng ban đầu làm

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 [e. g. , bằng cách sử dụng phương thức trong mã dựa trên gọi lại]

Tuy nhiên, nếu không được chăm sóc đặc biệt, đối tượng ban đầu thường bị mất. Tạo một chức năng liên kết từ chức năng, sử dụng đối tượng ban đầu, giải quyết gọn gàng vấn đề này

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
9

Ghi chú. Nếu bạn chạy ví dụ này ở chế độ nghiêm ngặt [e. g. trong các mô-đun ECMAScript hoặc thông qua chỉ thị

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
53], giá trị toàn cầu
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 sẽ không được xác định, khiến lệnh gọi
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
55 không thành công

Nếu bạn chạy cái này trong mô-đun Node CommonJS, thì phạm vi hàng đầu

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 sẽ trỏ đến
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
57 thay vì
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
58, bất kể có ở chế độ nghiêm ngặt hay không. Tuy nhiên, trong các hàm, tham chiếu của
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 không ràng buộc vẫn tuân theo quy tắc "
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
58 không nghiêm ngặt,
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
0 nghiêm ngặt". Do đó, ở chế độ không nghiêm ngặt [mặc định],
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
55 sẽ trả về
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
0 vì
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
64 đang ghi vào một đối tượng khác [
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
57] từ những gì mà
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
66 đang đọc từ [
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
58]

Trên thực tế, một số "phương thức" tích hợp cũng là các getters trả về các hàm bị ràng buộc — một ví dụ đáng chú ý là

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
68, khi được truy cập, trả về một hàm bị ràng buộc mà bạn có thể chuyển trực tiếp dưới dạng gọi lại

Các chức năng được áp dụng một phần

Cách sử dụng đơn giản nhất tiếp theo của

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
4 là tạo một hàm với các đối số ban đầu được chỉ định trước

Các đối số này [nếu có] tuân theo giá trị

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 được cung cấp và sau đó được chèn vào đầu các đối số được truyền cho hàm đích, theo sau là bất kỳ đối số nào được truyền cho hàm bị ràng buộc tại thời điểm nó được gọi

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5

Với setTimeout[]

Theo mặc định, trong

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
41, từ khóa
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 sẽ được đặt thành
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
58, là
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
44 trong trình duyệt. Khi làm việc với các phương thức lớp yêu cầu
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 tham chiếu đến các thể hiện của lớp, bạn có thể ràng buộc rõ ràng
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 với hàm gọi lại, để duy trì thể hiện

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
6

Bạn cũng có thể sử dụng các chức năng mũi tên cho mục đích này

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
4

Các hàm ràng buộc được sử dụng làm hàm tạo

Các hàm giới hạn tự động phù hợp để sử dụng với toán tử

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
1 để xây dựng các thể hiện mới được tạo bởi hàm đích. Khi một chức năng ràng buộc được sử dụng để xây dựng một giá trị,
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 được cung cấp sẽ bị bỏ qua. Tuy nhiên, các đối số được cung cấp vẫn được thêm vào lệnh gọi hàm tạo

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
4

Lưu ý rằng bạn không cần làm gì đặc biệt để tạo một hàm liên kết để sử dụng với

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
1.
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
47,
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
91,
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5, v.v. tất cả đều hoạt động như mong đợi, như thể hàm tạo không bao giờ bị ràng buộc. Sự khác biệt duy nhất là nó không còn có thể được sử dụng cho
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
90

Hệ quả là bạn không cần làm bất cứ điều gì đặc biệt để tạo một hàm liên kết được gọi rõ ràng, ngay cả khi bạn muốn yêu cầu hàm liên kết chỉ được gọi bằng cách sử dụng

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
1. Nếu bạn gọi nó mà không có
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
1, thì đột nhiên
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 bị ràng buộc không bị bỏ qua

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
0

Nếu bạn muốn hạn chế một chức năng bị ràng buộc chỉ có thể gọi được với

class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
1 hoặc chỉ có thể gọi được mà không có
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
1, chức năng mục tiêu phải thực thi hạn chế đó, chẳng hạn như bằng cách kiểm tra
class Base {
  constructor[...args] {
    console.log[new.target === Base];
    console.log[args];
  }
}

const BoundBase = Base.bind[null, 1, 2];

new BoundBase[3, 4]; // true, [1, 2, 3, 4]
49 hoặc sử dụng một lớp thay thế

các lớp ràng buộc

Sử dụng

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
4 trên các lớp bảo tồn hầu hết ngữ nghĩa của lớp, ngoại trừ việc tất cả các thuộc tính tĩnh của lớp hiện tại bị mất. Tuy nhiên, vì chuỗi nguyên mẫu được giữ nguyên nên bạn vẫn có thể truy cập các thuộc tính tĩnh được kế thừa từ lớp cha

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
1

Chuyển đổi các phương thức thành các chức năng tiện ích

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
4 cũng hữu ích trong trường hợp bạn muốn chuyển đổi một phương thức yêu cầu một giá trị
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 cụ thể thành một hàm tiện ích đơn giản chấp nhận tham số
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 trước đó làm tham số bình thường. Điều này tương tự như cách hoạt động của các chức năng tiện ích đa năng. thay vì gọi
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
04, bạn sử dụng
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
05, điều này tránh biến đổi
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
06 và cho phép bạn sử dụng
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
07 với các đối tượng dạng mảng không phải là mảng [ví dụ:
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
08]

Lấy ví dụ về

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
09 mà bạn muốn sử dụng để chuyển đổi một đối tượng dạng mảng thành một mảng thực. Bạn có thể tạo một lối tắt như thế này

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
2

Lưu ý rằng bạn không thể lưu

"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
10 và gọi nó là một hàm đơn giản, bởi vì phương thức
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
11 cũng đọc giá trị
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 của nó, đây là hàm mà nó nên gọi. Trong trường hợp này, bạn có thể sử dụng
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
4 để ràng buộc giá trị của
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 cho
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
11. Trong đoạn mã sau,
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
16 là phiên bản ràng buộc của
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
17, với giá trị
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
5 được ràng buộc với
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
09. Điều này có nghĩa là có thể loại bỏ các cuộc gọi
"use strict"; // prevent `this` from being boxed into the wrapper object

function log[...args] {
  console.log[this, ...args];
}
const boundLog = log.bind["this value", 1, 2];
const boundLog2 = boundLog.bind["new this value", 3, 4];
boundLog2[5, 6]; // "this value", 1, 2, 3, 4, 5, 6
11 bổ sung

Sự khác biệt giữa liên kết áp dụng và cuộc gọi là gì?

Tóm tắt. gọi. liên kết giá trị này, gọi hàm và cho phép bạn chuyển danh sách đối số. áp dụng. liên kết giá trị này, gọi hàm và cho phép bạn truyền đối số dưới dạng một mảng. trói buộc. liên kết giá trị này, trả về một hàm mới và cho phép bạn chuyển vào danh sách các đối số.

Tại sao chúng ta sử dụng phương thức liên kết áp dụng cuộc gọi trong JS?

call[] hoặc. apply[] khi bạn muốn gọi hàm ngay lập tức và sửa đổi ngữ cảnh. Gọi/áp dụng gọi hàm ngay lập tức, trong khi bind trả về một hàm mà khi được thực thi sau đó, sẽ có ngữ cảnh chính xác được đặt để gọi hàm ban đầu .

Cuộc gọi trong JavaScript là gì?

Phương thức call[] là một phương thức JavaScript được xác định trước . Nó có thể được sử dụng để gọi [gọi] một phương thức với đối tượng chủ sở hữu làm đối số [tham số]. Với call[] , một đối tượng có thể sử dụng một phương thức thuộc đối tượng khác.

Liên kết trong JavaScript là gì?

Phương thức bind[] tạo một hàm mới, khi được gọi, từ khóa this của nó được đặt thành giá trị được cung cấp, với một chuỗi đối số nhất định trước bất kỳ đối số nào được cung cấp khi hàm mới . .

Công dụng của bind[] là gì?

Chúng tôi sử dụng phương thức Bind[] để gọi một hàm có giá trị this , từ khóa này đề cập đến cùng một đối tượng hiện đang được chọn. Nói cách khác, phương thức bind[] cho phép chúng ta dễ dàng thiết lập đối tượng nào sẽ bị ràng buộc bởi từ khóa this khi một hàm hoặc phương thức được gọi.

Chủ Đề