Kiểm tra xem chức năng có được gọi là javascript không

Phương thức call() gọi hàm với một giá trị this đã cho và các đối số được cung cấp riêng lẻ

call(thisArg)
call(thisArg, arg1)
call(thisArg, arg1, /* …, */ argN)

thisArg

Giá trị sử dụng là this khi gọi

function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
0. Nếu chức năng không ở chế độ nghiêm ngặt,
function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
1 và
function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
2 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

function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
3 Tùy chọn

Đối số cho chức năng

Kết quả của việc gọi hàm với giá trị và đối số this được chỉ định

Ghi chú. Hàm này gần giống với hàm

function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
5, ngoại trừ việc call() chấp nhận một danh sách đối số, trong khi
function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
5 chấp nhận một mảng đối số — ví dụ:
function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
8 so với.
function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
9

Thông thường, khi gọi hàm, giá trị của this bên trong hàm chính là đối tượng mà hàm được truy cập trên đó. Với call(), bạn có thể gán một giá trị tùy ý là this khi gọi một hàm hiện có mà không cần gắn hàm đó vào đối tượng làm thuộc tính trước. Điều này cho phép bạn sử dụng các phương thức của một đối tượng làm các hàm tiện ích chung

Cảnh báo. Không sử dụng call() để xây dựng chuỗi (ví dụ: để thực hiện kế thừa). Điều này gọi hàm tạo như một hàm đơn giản, có nghĩa là

globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"
4 là
function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
2 và các lớp sẽ đưa ra lỗi vì chúng không thể được gọi nếu không có
globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"
6. Sử dụng
globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"
7 hoặc
globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"
8 để thay thế

Trong ví dụ dưới đây, khi chúng ta gọi

globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"
9, giá trị của this sẽ bị ràng buộc với đối tượng
"use strict";

globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // throws TypeError: Cannot read the property of 'globProp' of undefined
1, ngay cả khi
globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"
9 không phải là một phương thức của
"use strict";

globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // throws TypeError: Cannot read the property of 'globProp' of undefined
1

function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours

Nếu tham số thisArg đầu tiên bị bỏ qua, nó sẽ mặc định là

function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
2. Ở chế độ không nghiêm ngặt, giá trị this sau đó được thay thế bằng
"use strict";

globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // throws TypeError: Cannot read the property of 'globProp' of undefined
7 (tương tự như đối tượng toàn cầu)

globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // Logs "globProp value is Wisen"

Ở chế độ nghiêm ngặt, giá trị của this không được thay thế, do đó, nó vẫn là

function greet() {
  console.log(this.animal, "typically sleep between", this.sleepDuration);
}

const obj = {
  animal: "cats",
  sleepDuration: "12 and 16 hours",
};

greet.call(obj); // cats typically sleep between 12 and 16 hours
2

"use strict";

globalThis.globProp = "Wisen";

function display() {
  console.log(`globProp value is ${this.globProp}`);
}

display.call(); // throws TypeError: Cannot read the property of 'globProp' of undefined

call() gần như tương đương với một lệnh gọi hàm bình thường, ngoại trừ việc this được truyền dưới dạng tham số bình thường thay vì giá trị mà hàm được truy cập trên đó. Đ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

const slice = Array.prototype.slice;

// ...

slice.call(arguments);
2, bạn sử dụng
const slice = Array.prototype.slice;

// ...

slice.call(arguments);
3, điều này tránh biến đổi
const slice = Array.prototype.slice;

// ...

slice.call(arguments);
4 và cho phép bạn sử dụng
const slice = Array.prototype.slice;

// ...

slice.call(arguments);
5 với các đối tượng dạng mảng không phải là mảng (ví dụ:
const slice = Array.prototype.slice;

// ...

slice.call(arguments);
6)

Lấy ví dụ:

const slice = Array.prototype.slice;

// ...

slice.call(arguments);
7 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

const slice = Array.prototype.slice;

// ...

slice.call(arguments);

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

const slice = Array.prototype.slice;

// ...

slice.call(arguments);
8 và gọi nó là một hàm đơn giản, bởi vì phương thức call() cũng đọc giá trị this 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
// Same as "slice" in the previous example
const unboundSlice = Array.prototype.slice;
const slice = Function.prototype.call.bind(unboundSlice);

// ...

slice(arguments);
1 để ràng buộc giá trị của this cho call(). Trong đoạn mã sau,
// Same as "slice" in the previous example
const unboundSlice = Array.prototype.slice;
const slice = Function.prototype.call.bind(unboundSlice);

// ...

slice(arguments);
4 là một phiên bản liên kết của
// Same as "slice" in the previous example
const unboundSlice = Array.prototype.slice;
const slice = Function.prototype.call.bind(unboundSlice);

// ...

slice(arguments);
5, với giá trị this được liên kết với
const slice = Array.prototype.slice;

// ...

slice.call(arguments);
7. Điều này có nghĩa là các cuộc gọi call() bổ sung có thể được loại bỏ

Làm cách nào để kiểm tra xem hàm JavaScript có được gọi không?

Sử dụng thể hiện của Toán tử . Hàm, đối tượng, mảng, v.v. , là các loại đối tượng JavaScript. Vì vậy, lập trình viên có thể sử dụng nó với toán tử instanceof.

Làm cách nào để kiểm tra xem một hàm có chưa được xác định trong JavaScript không?

Hãy dùng thử. kiểm tra hàm(t) { if (t === undefined) { return 'Giá trị không xác định. '; . log(test(x)); // kết quả dự kiến. "Giá trị không xác định. "