Sunday 15 July 2012

javascript - What is the Protoype of an Object when the Object is created using Object Literal/ Object.Create -



javascript - What is the Protoype of an Object when the Object is created using Object Literal/ Object.Create -

can tell me prototype of simple object when object created using object literal notation/ object.create ? when went through mdn notes on object.create stated below

var = {a: 1}; // ---> object.prototype ---> null var b = object.create(a); // b ---> ---> object.prototype ---> null console.log(b.a); // 1 (inherited) var c = object.create(b); // c ---> b ---> ---> object.prototype ---> null var d = object.create(null); // d ---> null console.log(d.hasownproperty); // undefined, because d doesn't inherit object.prototype

but whenever tried console.log(b.prototype) gave me undefined, same case of "c" , "d" object. confused here, if b's prototype "a" according mdn, why giving undefined.

one more question, if b.prototype undefined, how come console.log(b.a); resulting 1. how inheriting in case ?

to prototype of object, utilize x.__proto__ or object.getprototypeof(x) rather x.prototype. note __proto__ deprecated in favor of object.getprototypeof().

the property using, x.prototype property found on functions. used build __proto__ when function called constructor using new.

javascript

No comments:

Post a Comment