我正在使用“调用”方法来触发函数并更改范围,但是这样做之后如何获得原始范围?换句话说,如何访问“基础”上下文?
例如,我调用“joe”并将“jill”作为上下文传递。但是,在“问候”的第二个警报中,如何访问“名称:乔”变量?
var joe = {
name : "Joe",
greet : function (name) {
alert(this.name + " says hello to " + name);
//Below is wrong. How do I get "Joe"?
alert('Original Joe context name is: ' + this.name);
}
};
var jill = {
name : "Jill"
};
//Outputs "Jill says hello to Mike"
joe.greet.call(jill, "Mike");
这是此示例的 jsFiddle:http: //jsfiddle.net/ueeRk/