函数的原型属性

时间:2021-05-31 05:03:31

标签: javascript prototypal-inheritance function-prototypes

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

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

但是当我对 Arrays 等其他对象执行相同的操作时,它向我展示了 Array 的实际原型,其中所有方法都链接在一起

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

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

1 个答案:

答案 0 :(得分: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

相关问题