admin管理员组文章数量:1405586
We can use Object.prototype.toString.call(foo)
to detect object class (the type of foo), and it works well.
But why does Object.toString.call({})
throw TypeError: Function.prototype.toString is not generic
?
Doesn't Object.toString
inherit from Object.prototype
?
We can use Object.prototype.toString.call(foo)
to detect object class (the type of foo), and it works well.
But why does Object.toString.call({})
throw TypeError: Function.prototype.toString is not generic
?
Doesn't Object.toString
inherit from Object.prototype
?
- 1 Because toString is a prototype method and not a function static method. – Nagaraj Tantri Commented Oct 13, 2014 at 6:15
-
1
Object
is aFunction
, whileObject.prototype
is the object which everything inherits from. – Derek 朕會功夫 Commented Oct 13, 2014 at 6:29
1 Answer
Reset to default 12Doesn't Object.toString inherit from Object.prototype
No. The built–in Object constructor is a Function (like all native constructors), so it inherits from Function.prototype (i.e. its private [[Prototype]]
property references Function.prototype) before its own prototype property.
Its prototype chain is:
Object[[Prototype]] -> Function.prototype -> Object.prototype -> null
so Function.prototype.toString masks Object.prototype.toString.
A bit of trivia: note that while Function.prototype is a function, it doesn't inherit from itself but from Object.prototype.
本文标签: javascriptdifferent between ObjecttoString and ObjectprototypetoStringStack Overflow
版权声明:本文标题:javascript - different between Object.toString and Object.prototype.toString - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744340147a2601428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论