1

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

raziq ijr
  • 15
  • 4

2 Answers2

1

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

Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
0

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.

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531