example = {};
example.Math = {
sin: function() {console.log('sin');},
cos: function() {console.log('cos');}
};
foo = function(){
sin();
};
foo.prototype = window.example.Math;
console.log(foo.prototype)
console.log(foo.cos)
console.log(foo())
From what I thought I understood references are searched for through the scope chain. So theoretically if I assign an object with functions defined to a prototype I should get all of those functions and fields of the object or so I thought. Where is the error in my understanding? Neither of the above scenarios work. Additionally the internal proto variable is not being updated. Obviously that’s why it is not working, but why doesn’t my assignment to the prototype work?
I think you need to realize that the prototyped method needs to be run with the
newkeyword, and it needs to usethisto access its variables and methods, like this: