Here is an object:
var obj = new function(){
this.prop = {};
}
and I try to do something like:
obj.prop.prototype["new_attr"] = "some_value";
What I’d like to do, is permanently modify obj.prop to contain the new attributes. From what I understood, all “Objects” had the prototype, but JavaScript is telling me prop doesn’t have prototype.
Maybe I’m taking the wrong approach (in trying to permanently modify prop), but I’d at least like to know why the code above doesn’t work.
Typically you access the
prototypeobject from the constructor function, but you can get it from an object as well, but not using.prototype.The unofficial way (not supported in all browsers) to get the prototype object of an object is with the
__proto__property. (I believe it is deprecated in the browsers that support(ed) it).The official ECMAScript 5 way (also not supported in all browsers) to get the prototype object of an object is to use
Object.getPrototypeOf().In your case
.propis referencing an object literal, so you already know that the prototype isObject.prototype.Note that it is almost always a bad idea to extend
Object.prototype.If you’re just trying to modify the object referenced by
.prop, then simply do: