【问题标题】:Prototype property of functions函数的原型属性
【发布时间】:2021-05-31 05:03:31
【问题描述】:

当我尝试访问函数的原型属性时,它给了我以下结果:

console.log(Function.prototype); //  () { [native code] }

但是当我对 Arrays 等其他对象执行相同的操作时,它会显示所有方法都链接的 Array 的实际原型

console.log(Array.prototype); //  [constructor: ƒ, concat: ƒ, copyWithin: ƒ, fill: ƒ, find: ƒ, …]

我真正想知道的是为什么Functions 的行为不同。

【问题讨论】:

  • 你在哪里看到这个输出? console.log(Function.prototype); shows me this 在 Firefox 中。

标签: javascript prototypal-inheritance function-prototypes


【解决方案1】:

使用console.log 记录函数将记录函数的文本。如果函数不是用 JavaScript 编写的——例如,像这里一样,如果实现是由环境提供的——它会给你[native code]

但有一种简单的方法可以记录函数的属性,至少在 Chrome 中是这样:使用console.dir

console.dir(Function.prototype)
<img src=" https://i.stack.imgur.com/6GEij.png">

它为您提供了预期的函数属性,至少在 Chrome 和 FF 中都是这样。

Function.prototype 的功能描述为here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    相关资源
    最近更新 更多