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?

Share Improve this question edited Oct 13, 2014 at 6:18 GregL 38.2k8 gold badges64 silver badges68 bronze badges asked Oct 13, 2014 at 6:10 user3928561user3928561 931 silver badge6 bronze badges 2
  • 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 a Function, while Object.prototype is the object which everything inherits from. – Derek 朕會功夫 Commented Oct 13, 2014 at 6:29
Add a ment  | 

1 Answer 1

Reset to default 12

Doesn'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