【发布时间】:2014-03-06 04:30:49
【问题描述】:
为什么“show”方法无法在此处访问“text”变量?
// @constructor A
var A = function(){
//alerts some text variable
this.show = function(){
alert("Hello")
alert(text);
}
}
//@constructor B
var B = function(){
//local text variable
var text = "World";
A.apply(this); // "show" method will get attached to B
}
var obj = new B();
//now calling show of B's object.
obj.show(); //returns error.
//Expected output
alerts "Hello"
alerts "World"
//Actual output
alerts "Hello"
ReferenceError: text is not defined
我在这里遗漏了什么吗? B的“show”方法不应该可以访问“text”变量吗?
【问题讨论】:
标签: javascript variables inheritance scope apply