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.
Asked
Active
Viewed 73 times
0

chetan mehta
- 369
- 1
- 3
- 13
-
Please post your code, I don't understand a thing... – elclanrs Jul 11 '13 at 07:31
-
@elclanrs- There is no code .Its a general question.there could be problem with in language but the question clear in my mind. – chetan mehta Jul 11 '13 at 07:54
-
I'm not realy sure what the question is either but here you can find some detail about what prototype is: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 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()` – HMR Jul 11 '13 at 09:31
1 Answers
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

steveluscher
- 4,144
- 24
- 42
-
It is written in Javascript. Copy and paste the code into this to watch it run: http://repl.it/languages/JavaScript – steveluscher Jul 11 '13 at 08:26
-
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();}` – HMR Jul 11 '13 at 09:27