Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different between Object.toString and Object.prototype.toString

Tags:

javascript

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?

like image 253
user3928561 Avatar asked Oct 13 '14 06:10

user3928561


1 Answers

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.

like image 158
RobG Avatar answered Oct 21 '22 06:10

RobG