The first lesson is scope.
var scope = "global";
function Scope() {
var scope = "local";
this.s = function() { return scope; }
}
var instance = new Scope();
console.log(instance.scope); ///this is 'undefined'
console.log(instance.s()); /// this is 'local'
This makes sense in the world of closures.
Then BaseScope
is introduced.
function BaseScope() {
this.scope = "parent";
this.h = function() {return "Hello";}
}
Scope.prototype = Object.create(BaseScope);
So now, I want to add the prototype of BaseScope
to Scope
. The below, is what I WANT to be returned.
var instance2 = new Scope();
console.log(instance2.s()); ///Should be returning 'local' because it is the fastest returned s() function on the object's prototype.
console.log(instance2.scope); ///Should be returning BaseScope's scope, since it is technically the only defined 'scope' property up the chain.
console.log(instance2.h()); ///Should return "Hello"
The last example is returning undefined
which makes me think I am doing something completely wrong. What steps should I take to get the desired effect above?