多个变量声明,用于添加对父对象的引用

时间:2015-01-04 10:39:08

标签: javascript jquery

作为样本,

我有一个对象,它有很多方法。每个方法都拥有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();

Live

0 个答案:

没有答案