when we create an object like this
function Person(first,last){
this.first = first;
this.last = last;
this.full = function (){
alert(this.first + " " + this.last);
}
}
obj = new Person('abdul','raziq');
could we also add to obj's prototype anything like this
obj.prototype = 'some functions or anything ';
or its not possible once we created the object ?
and there is a __proto__ property on person object
obj.__proto__
but when i access obj.prototype property its undefined ?
can someone pls explain in a simple way possible
2 Answers 2
The prototype property only exists on functions, not on instances of functions. Read this StackOverflow answer to know more: https://stackoverflow.com/a/8096017/783743
Comments
You can do something like
Person.prototype.full = function(){
alert(this.first + " " + this.last);
}
Demo: Fiddle
The prototype object is attached to the Class not to the instance so yes you can add/remove properties to/from prototype after instances are created. And all instances of the type will reflect the changes made.
prototypeproperty. Add it to the prototype of the constructor function instead.