Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why isn't Object.prototype === to myNewObj.prototype?

So if you look at this fiddle http://jsfiddle.net/r0k3t/z8f2N/1/ you can see that

var me = { fName: "ken", lName: "n" };

console.log(Object.prototype === Object.getPrototypeOf(me));

returns true. Why doesn't

console.log(Object.prototype === me.prototype);

Given that I created the "me" object as an object literal sure enough it's prototype should be Object.prototype and the first line would seem to confirm that.

like image 803
Kenn Avatar asked Dec 19 '12 15:12

Kenn


People also ask

How do you set the prototype of one object to another?

The Object. setPrototypeOf() method sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null. All JavaScript objects inherit properties and methods from a prototype. It is generally considered the proper way to set the prototype of an object.

What is difference between __ proto __ and prototype?

prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.

Do object literals have prototype?

The object literals are using undefined and number 15 to setup __proto__ value. Because only an object or null are allowed to be prototypes, objUndefined and objNumber still have their default prototypes: plain JavaScript objects {} .

Is __ proto __ deprecated?

prototype. __proto__ Deprecated: This feature is no longer recommended.


1 Answers

Object.prototype === me.constructor.prototype; // true

I let you guess now how getPrototypeOf works :-)

Also, the non-standard-yet-but-works-almost-everywhere solution (thanks jAndy):

Object.prototype === me.__proto__; // true
like image 98
Florian Margaine Avatar answered Sep 21 '22 21:09

Florian Margaine