美文网首页
Function.prototype.call()

Function.prototype.call()

作者: black墨 | 来源:发表于2021-09-02 13:13 被阅读0次

本文不构成任何指导学习意见,记录仅供学习以及知识点查看

文章来源: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript
每个 JavaScript 函数实际上都是一个 Function 对象。运行 (function(){}).constructor === Function // true 便可以得到这个结论。

含义示例

call() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数。

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

console.log(new Food('cheese', 5).name);
// expected output: "cheese"

语法

function.call(thisArg, arg1, arg2, ...)

参数

thisArg

可选的。在 function 函数运行时使用的 this 值。请注意,this可能不是该方法看到的实际值:如果这个函数处于非严格模式下,则指定为 nullundefined 时会自动替换为指向全局对象,原始值会被包装。</dd>

arg1, arg2, ...

指定的参数列表

返回值

使用调用者提供的 this 值和参数调用该函数的返回值。若该方法没有返回值,则返回 undefined

相关文章

网友评论

      本文标题:Function.prototype.call()

      本文链接:https://www.haomeiwen.com/subject/nnnqwltx.html