0

I know this Question might have been asked already in different ways on stackoverflow but still
I just need to clarify my doubt
There is one prop in Object Constructor namely prototype. And there is Object.prototype Object.
So when i am writing something as Object.prototype=object2
Am I setting the prototype property on the Object Constructor or Object.prototype Object is getting the values from object2 by reference.

3
  • Please post your code, I don't understand a thing... Commented Jul 11, 2013 at 7:31
  • @elclanrs- There is no code .Its a general question.there could be problem with in language but the question clear in my mind. Commented Jul 11, 2013 at 7:54
  • I'm not realy sure what the question is either but here you can find some detail about what prototype is: stackoverflow.com/questions/16063394/… The constructor property should point to the function that constructed the object instance var anObject=new Object();anObject.constructor === Object;//true var anotherObject=new anObject.constructor() Commented Jul 11, 2013 at 9:31

1 Answer 1

1

You are setting the prototype of Object to object2, by reference.

var dogProto = {
  bark: 'woof'
};

// Define a Dog class
var Dog = (function() {});

// Set its prototype to that which is contained in proto
Dog.prototype = dogProto;

// Make a Dog
var fido = new Dog;

// What's the bark property of fido?
console.log(fido.bark); // outputs: woof

// Modify the original dogProto object
dogProto.bark = dogProto.bark.toUpperCase();

// What's the bark property of fido now?
console.log(fido.bark); // outputs: WOOF
Sign up to request clarification or add additional context in comments.

2 Comments

It is written in Javascript. Copy and paste the code into this to watch it run: repl.it/languages/JavaScript
Setting prototype of Dog in this way will have fido.constructor point to Object() instead of Dog Dog.prototype.bark="woof" would be saver if you want to use constructor at some point. Like Dog.prototype.haveAKid=function(){return new this.constructor();}

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.