javascript - How to tell if an object was created with 'Object.create' or using literal syntax/'new' keyword -


i'm trying figure out if there way tell if object created using object.create() or using new keyword / literal syntax. because objects created object.create() not take properties off prototype property (they sort of copy properties) , made new , literal syntax take properties prototype property, seems difficult tell how object created. example:

function meat() {}; var beef = new meat(); var tuna = object.create(meat);  console.log(object.getprototypeof(tuna));//=> [function: meat] console.log(object.getprototypeof(beef));//=> {}  console.log(tuna.constructor.prototype);//=> [function] console.log(beef.constructor.prototype);//=> {}  console.log(tuna.prototype);//=> {} console.log(beef.prototype);//=> undefined  console.log(tuna.__proto__);//=> object console.log(beef.__proto__);//=> object  console.log(typeof tuna);//=> object console.log(typeof beef);//=> object  console.log(meat.prototype.isprototypeof(tuna));//=> false console.log(meat.prototype.isprototypeof(beef));//=> true    

there doesn't appear way tell method object inherited it's properties. ideas?

object.create generic object-construction operation. there's no way tell whether object created object.create because there's no reliable feature of such objects (other can't native objects or proxies).

and not true @ object.create “just sort of copies properties” — sets [[prototype]] (aka __proto__) reference value provide. “copy”-like behavior if specify optional second argument, takes property descriptors.

i note you've made 1 mistake might confusing issue you:

 var tuna = object.create(meat); 

if meant “make object akin result of new meat()”, wrong. want instead:

var tuna = object.create(meat.prototype); 

because parameter object.create is new object's prototype, not constructor. object doing react same instanceof, isprototypeof, et cetera new meat(); won't have had meat constructor run on it, won't have properties such objects do.

since example constructor empty, once you've fixed this, 2 objects have same responses various tests. doesn't tell difference (because can't), should not need to tell difference.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -