作为样本,
我有一个对象,它有很多方法。每个方法都拥有private
个函数。当我需要引用我的对象父母时,通常我喜欢这样:
var that = this
- 效果很好。
但是如果有大量的话可以避免这种情况吗?
示例:
var x = function () {
return {
init:function () {
this.val = 10;
},
fun1 : function () {
var that = this;
var p = function () {
console.log(this.val) //not works i know
console.log(that.val) //not works
}
},
fun2 : function () {
var that = this;
var p = function () {
console.log(this.val) //not works i know
console.log(that.val) //not works
}
},
fun3 : function () { // it keep grow to 100's..!?
var that = this;
var p = function () {
console.log(this.val) //not works i know
console.log(that.val) //not works
}
}
}
}();
x.init();