2
我在Javascript中很新,所以我可能不会使用确切的术语。关闭和其他方法定义的方法问题
假设我定义了一个对象字面。
var myObj = {
someMethod:function() {
//can we have access to "someValue" via closure?
alert(someValue);
}
}
然后我们把这个函数赋值给另一个这样的对象。
var myOtherObject = {
someOtherMethod:function() {
var someValue = 'Hello World';
//If we did this, then the function would have access to "someValue"
this.aMethod = function() {
alert(someValue);
}
//This does not work for "someMethod" to have access to "someValue"
//this.someMethod = myObj.someMethod;
//This does work, however I would like to avoid the use of eval()
this.someMethod = eval("("+myObj.someMethod.toString()+")");
}
}
是否有可能有 myOtherObject.someMethod()工作,而无需使用的eval()如上?