admin管理员组文章数量:1287913
console.log(typeof String.prototype); // object
console.log(typeof Number.prototype); // object
console.log(typeof Object.prototype); // object
console.log(typeof Boolean.prototype); // object
console.log(typeof Function.prototype); // function
Why does typeof Function.prototype
return "function", not "object" like other prototype objects?
Thank you!
console.log(typeof String.prototype); // object
console.log(typeof Number.prototype); // object
console.log(typeof Object.prototype); // object
console.log(typeof Boolean.prototype); // object
console.log(typeof Function.prototype); // function
Why does typeof Function.prototype
return "function", not "object" like other prototype objects?
Thank you!
Share Improve this question edited Oct 26, 2017 at 19:50 Andrew Truckle 19.1k17 gold badges82 silver badges215 bronze badges asked Feb 1, 2011 at 5:38 weilouweilou 4,61711 gold badges45 silver badges56 bronze badges 4- 5 You could have asked the question without all the HTML. A good question though... – Ateş Göral Commented Feb 1, 2011 at 5:41
- 1 @AtesGoral Fixed now! – doubleOrt Commented Oct 26, 2017 at 19:58
- 1 @Taurus Better late (6 years) than sorry! – Ateş Göral Commented Oct 27, 2017 at 23:14
- 1 @AtesGoral indeed. – doubleOrt Commented Oct 27, 2017 at 23:55
4 Answers
Reset to default 17This seems to be defined in ECMAScript 5:
15.3.4 Properties of the Function Prototype Object
The Function prototype object is itself a Function object (its
[[Class]]
is "Function") that, when invoked, accepts any arguments and returns undefined.
Its mentioned in the ECMAScript2015
http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-prototype-object :
Th 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.
This function object does not actually do anything that a function object is meant to do. You can call it with any arguments & it returns undefined. It is a dumb wrt. function object. It's a normal prototype object.
And since it's just there for compatibility reasons, it does not even has a prototype property.
For more elaboration, you can refer this answer: enter link description here
Because function is a native object which among other properties has internal [[Construct]] and [[Call]] properties and also explicit prototype property — the reference to a prototype of the future objects. And its class is function.
F.[[Class]] = "Function"
F.[[Call]] = <reference to function> // function itself
Thus [[Call]] besides the [[Class]] property (which equals to "Function") is the main in respect of objects distinguishing. Therefore the objects having internal [[Call]] property are called as functions. The typeof operator for such objects returns "function" value.
see for reference
Since it has all the methods and props that any function is ought to have, this makes it effectively a function ...
Think about it for a moment and let it sink and you'll get the picture by then :)
本文标签:
版权声明:本文标题:In JavaScript, why typeof Function.prototype is "function", not "object" like other prototyp 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738234063a2070607.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论