Can anyone tell me why this code does not work?
PhoneNumber.prototype = Map;
var Map = {
writeToDOM : function(container) {
//DO STUFF HERE
}
};
PhoneNumber.writeToDOM(Container);
Is it not possible to reference an object in respect to an object’s protype and use said object’s functions? The reason I would want to do this is so I can use the Map variable on multiple objects rather than having to repeat the writeToDOM code for each object like this:
Object1.prototype.writeToDOM = function() {
//DO STUFF HERE
}
Object2.prototype.writeToDOM = function() {
//DO the exact same stuff as Object1
}
Your code is equivalent to the following:
That is, you are assigning
PhoneNumber.prototypetoMap, which currently containsundefined. Then you are reassigningMapto a new value, but not changingPhoneNumber.prototypeat all.If you simply assigned
Mapto the desired object before assigning the prototype toMap, you would be OK: