Prototype extension on object literal

If I have the following code, why it returns the error message “Unable to set property“ second_prop ”from undefined. I thought you could extend the prototype property and add more variables and methods to the object prototype. Since the two console statements return 'Object' and true, then why does it return an undefined error. My thinking is that if obj is an object of type Object, should I be able to do temp.prototype.newproperty? So, the object will have "newproperty". But I obviously I’m mistaken, so there is something missing for me. e of why I need Object.create (), where obj is already an object literal? Is this no longer the object? I'm just looking at some examples and try to understand it.

var obj = { first_property: 'first property' } console.log(typeof obj); console.log(obj instanceof Object); var temp = Object.create(obj); temp.prototype.second_prop = 'second property' 

Exit

 //object //true //Uncaught TypeError: Cannot set property 'second_prop' of undefined 

So why can't I do temp.prototype or obj.prototype?

0
source share
3 answers

If I have the following code, why does it return an error saying Cannot set property "second_prop" from undefined

The literal Javascript object is already an instance of the object and does not have the .prototype property. This property is in the constructor function, and not in the already created object. In any current browser (IE9 +), you can use Object.getPrototypeOf() to get a prototype of an already constructed object.

But it looks like you are likely to clone an object, and then add the properties of the clone. Or, if you want both objects to have the same additional property, add a property and then clone it. Or, if you just want to use your literal object, just use it as is.

Once an object has already been created, the prototype is an internal property, and it is usually not intended for you to mess with it. If you are going to change the prototype for all objects, you must change the prototype on the constructor. If you want to change the properties only for this instance of the object, just change the properties of the object itself, not the prototype.

As always, if you describe what you are actually trying to accomplish (instead of your own decision not fulfilling what you expected), we can help you much better.

I thought you could extend the prototype property and add more variables and methods to the prototype object.

You can, but the .prototype property is in the constructor, and not on an already constructed object.

Since the two console statements return "Object" and true, why does it return an undefined error. I think that if the "obj" is an object of type Object, then should I be able to do temp.prototype.newproperty?

You are misleading an instance object that is a prototype constructor. A particular object has a prototype as an internal property, and not as a public .prototype property. So, when instanceof says something, it means that it is literally an instance of this type of object (and thus the prototype of this type of objects in its internal prototype chain), and not that it is a constructor with the public property .prototype .

Even more, why do I need Object.create () when obj is already a literal object? Isn't that an object anymore?

The goal of Object.create() is to create a new object using an existing object as a prototype. If you declare a Javascript letter, and you just want to use the literal as an object in itself, then there is no reason to use Object.create() on it. Just use the literal as the already constructed object that it is.

+2
source

temp has no prototype. The object has a prototype. You can change the prototype for all objects by calling Object.prototype , but you cannot change it for just one object.

0
source

.prototype is a property that is not always associated with the actual prototype of an object. The real prototype can be obtained through .__proto__

 var obj = { first_property: 'first property' } console.log(typeof obj); console.log(obj instanceof Object); var temp = Object.create(obj); temp.__proto__.second_prop = 'second property'; console.log(Object.getPrototypeOf(temp)); //{ first_property: 'first property', //second_prop: 'second property' } 
0
source

All Articles