gpt4 book ai didi

javascript - 为什么 typeof(Function.prototype) 是函数

转载 作者:行者123 更新时间:2023-11-29 17:55:17 25 4
gpt4 key购买 nike

我知道原型(prototype)是对象字面量这一事实。因此可以在它们上定义方法和属性。 Function.prototype 有一些方法,如 apply、call、bind、toString 等。所以我认为函数的原型(prototype)应该是对象字面量。但是我运行了下面的代码,发现 Function.prototypetype function !

console.log(typeof(Function.prototype)); // function

为什么它本身不是对象字面量?

最佳答案

来自 the specification :

The Function prototype object is the intrinsic object %FunctionPrototype%. The Function prototype object is itself a built-in function object. When invoked, it accepts any arguments and returns undefined. It does not have a [[Construct]] internal method so it is not a constructor.

NOTE

The Function prototype object is specified to be a function object to ensure compatibility with ECMAScript code that was created prior to the ECMAScript 2015 specification.

(我的重点)

如果我们转到 ES5 规范,it says :

The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined.

...没有提供任何解释为什么会这样。该语言在 ES1、ES2、ES3 和 ES5 中基本没有变化。我认为最初的想法基本上是赋予它功能的原因,尽管 typeof(甚至在 ES1 中)没有查看内部 [[Class]],它查看是否实现了 [[Call]](现在仍然如此)。当事情一直追溯到 ES1 时,人们经常不得不调用“因为 Eich 在 10 天内完成了第一个 JavaScript,是的,当你这样做时会发生奇怪的事情”的论点。 :-)


旁注:我认为“对象字面量”是指“普通对象”。 (“对象文字”——规范中称为 object initializer 的东西——只是一种在源代码中编写对象的方法。还有其他创建普通对象的方法。)

关于javascript - 为什么 typeof(Function.prototype) 是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39698919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com