Hi I have this two objects in javascript
var john = { firstname: 'John', lastname: 'Smith' }
var jane = { firstname: 'Jane' }
doing this:
jane.__proto__ = john;
I can access to jane's properties and also john's properties
If __proto__
is not supported in IE8 for example, what's the equivalent to write this:
jane.__proto__ = john;
Thanks!
There is no equivalent or standard mechanism in IE. (The
__proto__
property in Firefox is non-standard extension as is not specified in the ECMAScript standards.)The [[prototype]] object can only be specified by setting the
prototype
property upon the function-object which acts as the constructor before the construction of a new object. The [[prototype]] can, however, be mutated later.Anyway, here is a little example of specifying a [[prototype]] from an existing object. Note that the [[prototype]] assignment must be done before the new object is created. ECMAScript 5th edition introduces Object.create which can do the following and shallow-clone an object.
__proto__
currently is not an standard use.Following the ECMAScript standard, the notation someObject.[[Prototype]] is used to designate the prototype of someObject. Since ECMAScript 5, the [[Prototype]] is accessed using the accessors
Object.getPrototypeOf()
andObject.setPrototypeOf()
.So you could use
getPrototypeOf()
to access.As in chrome,