0

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

6
  • The answer is yes, you can add to the prototype after an object instance is created. Commented Mar 12, 2013 at 5:17
  • i have modified my question pls read it once again Commented Mar 12, 2013 at 5:18
  • i want to add to obj prototype property , is it possible to add to obj prototype property something Commented Mar 12, 2013 at 5:19
  • 1
    objects does not have prototype property Commented Mar 12, 2013 at 5:20
  • Only functions have a prototype property. Add it to the prototype of the constructor function instead. Commented Mar 12, 2013 at 5:21

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

sorry i have corrected my question pls read it once again thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.