第一课是范围。
toJson()
这在封闭世界中是有道理的。
然后引入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'
。
BaseScope
现在,我想将function BaseScope() {
this.scope = "parent";
this.h = function() {return "Hello";}
}
Scope.prototype = Object.create(BaseScope);
的原型添加到BaseScope
。以下是我想要返回的内容。
Scope
最后一个例子是返回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"
,这让我觉得我做错了。我应采取哪些措施才能达到预期效果?