Metode call() memanggil fungsi dengan nilai this yang diberikan dan argumen yang diberikan secara individual
call(thisArg) call(thisArg, arg1) call(thisArg, arg1, /* …, */ argN)
thisArg
Nilai untuk digunakan sebagai this saat menelepon 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. Jika fungsi tidak dalam mode ketat, 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 dan 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 akan diganti dengan objek global, dan nilai primitif akan diubah menjadi objek
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 OpsionalArgumen untuk fungsi
Hasil pemanggilan fungsi dengan nilai dan argumen this yang ditentukan
Catatan. Fungsi ini hampir identik dengan 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, kecuali bahwa call() menerima daftar argumen, sementara 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 menerima satu larik argumen — misalnya, 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 vs. 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
Biasanya, saat memanggil suatu fungsi, nilai this di dalam fungsi adalah objek tempat fungsi itu diakses. Dengan call(), Anda dapat menetapkan nilai arbitrer sebagai this saat memanggil fungsi yang ada, tanpa terlebih dahulu melampirkan fungsi ke objek sebagai properti. Ini memungkinkan Anda untuk menggunakan metode satu objek sebagai fungsi utilitas umum
Peringatan. Jangan gunakan call() untuk merangkai konstruktor (misalnya, untuk mengimplementasikan pewarisan). Ini memanggil fungsi konstruktor sebagai fungsi biasa, yang berarti globalThis.globProp = "Wisen"; function display() { console.log(`globProp value is ${this.globProp}`); } display.call(); // Logs "globProp value is Wisen" 4 adalah 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, dan kelas melontarkan kesalahan karena tidak dapat dipanggil tanpa globalThis.globProp = "Wisen"; function display() { console.log(`globProp value is ${this.globProp}`); } display.call(); // Logs "globProp value is Wisen" 6. Gunakan globalThis.globProp = "Wisen"; function display() { console.log(`globProp value is ${this.globProp}`); } display.call(); // Logs "globProp value is Wisen" _7 atau globalThis.globProp = "Wisen"; function display() { console.log(`globProp value is ${this.globProp}`); } display.call(); // Logs "globProp value is Wisen" 8 sebagai gantinya
Dalam contoh di bawah ini, ketika kita memanggil globalThis.globProp = "Wisen"; function display() { console.log(`globProp value is ${this.globProp}`); } display.call(); // Logs "globProp value is Wisen" _9, nilai this akan terikat ke objek "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, bahkan ketika globalThis.globProp = "Wisen"; function display() { console.log(`globProp value is ${this.globProp}`); } display.call(); // Logs "globProp value is Wisen" 9 bukan metode "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 _
Jika parameter thisArg pertama dihilangkan, defaultnya adalah 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. Dalam mode non-ketat, nilai this_ kemudian diganti dengan "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 (yang mirip dengan objek global)
globalThis.globProp = "Wisen"; function display() { console.log(`globProp value is ${this.globProp}`); } display.call(); // Logs "globProp value is Wisen" _
Dalam mode ketat, nilai this_ tidak diganti, sehingga tetap sebagai 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() hampir setara dengan panggilan fungsi normal, kecuali bahwa this diteruskan sebagai parameter normal alih-alih sebagai nilai fungsi diakses. Ini mirip dengan cara kerja fungsi utilitas tujuan umum. alih-alih memanggil const slice = Array.prototype.slice; // ... slice.call(arguments); 2, Anda menggunakan const slice = Array.prototype.slice; // ... slice.call(arguments); 3, yang menghindari mutasi const slice = Array.prototype.slice; // ... slice.call(arguments); 4, dan memungkinkan Anda untuk menggunakan const slice = Array.prototype.slice; // ... slice.call(arguments); 5 dengan objek mirip larik yang bukan larik (misalnya, const slice = Array.prototype.slice; // ... slice.call(arguments); 6)
Ambil const slice = Array.prototype.slice; // ... slice.call(arguments); _7, misalnya, yang ingin Anda gunakan untuk mengubah objek seperti array menjadi array nyata. Anda dapat membuat pintasan seperti ini
const slice = Array.prototype.slice; // ... slice.call(arguments);
Perhatikan bahwa Anda tidak dapat menyimpan const slice = Array.prototype.slice; // ... slice.call(arguments); _8 dan menyebutnya sebagai fungsi biasa, karena metode call() juga membaca nilai this, yang merupakan fungsi yang harus dipanggil. Dalam hal ini, Anda dapat menggunakan // Same as "slice" in the previous example const unboundSlice = Array.prototype.slice; const slice = Function.prototype.call.bind(unboundSlice); // ... slice(arguments); 1 untuk mengikat nilai this untuk call(). Dalam potongan kode berikut, // Same as "slice" in the previous example const unboundSlice = Array.prototype.slice; const slice = Function.prototype.call.bind(unboundSlice); // ... slice(arguments); _4 adalah versi terikat dari // Same as "slice" in the previous example const unboundSlice = Array.prototype.slice; const slice = Function.prototype.call.bind(unboundSlice); // ... slice(arguments); 5, dengan nilai this terikat pada const slice = Array.prototype.slice; // ... slice.call(arguments); 7. Ini berarti panggilan call() tambahan dapat dihilangkan