简体   繁体   English

javascript object literal - 嵌套函数和“this”关键字

[英]javascript object literal - nested functions and the “this” keyword

In the example below, when functionA() is invoked, the this keyword refers to the containing object, so I can access its properties (eg theValue ) 在下面的示例中,当调用functionA()时, this关键字引用包含对象,因此我可以访问其属性(例如theValue

My question: How do I refer to properties of myObj from within the nested functionB() ? 我的问题:我如何从嵌套的 functionB()引用myObj属性?

var myObj = {
    theValue: "The rain in Spain", 
    functionA: function() {
        alert(this.theValue);
    },
    moreFunctions: {
        functionB: function() {
            alert(????.theValue);
        }
    }
}

myObj.functionA(); 
myObj.moreFunctions.functionB();  

Thanks in advance. 提前致谢。

Immediate invocation to the rescue! 立即援引救援!

var myObj = (function () {
    var that = {
        theValue: "The rain in Spain", 
        functionA: function() {
            alert(this.theValue); // or that.theValue, both work here
        },
        moreFunctions: {
            functionB: function() {
                alert(that.theValue);
            }
        }
    };
    return that;
}()); // <-- immediate invocation !!

You can decompose it even further: 您可以进一步分解它:

var myObj = (function () {
    function functionA() {
        alert(that.theValue);
    }
    function functionB() {
        alert(that.theValue);
    }
    var that = {
        theValue: "The rain in Spain", 
        functionA: functionA,
        moreFunctions: {
            functionB: functionB
        }
    }
    return that;
}());

If you're familiar with OOP, you can use this to make private variables. 如果您熟悉OOP,则可以使用它来创建私有变量。

You can simply use myObj : 你可以简单地使用myObj

alert(myObj.theValue);

Check demo http://jsbin.com/izugon/2/edit 查看演示http://jsbin.com/izugon/2/edit

A common practice is to define a "self" variable and use that rather than the this keyword. 通常的做法是定义一个“自”变量并使用它而不是this关键字。 This helps when you wish to add scope or create a class. 当您希望添加范围或创建类时,这会有所帮助。

var myObj = new function(){
    var self = this;
    this.theValue = "The rain in Spain";
    this.functionA = function() {
        alert(self.theValue);
    },
    this.moreFunctions = {
        functionB: function() {
            alert(self.theValue);
        }
    }
   }();

   myObj.functionA();
   myObj.moreFunctions.functionB();

Another possibility is to use the ECMA5 Function.prototype.bind method. 另一种可能性是使用ECMA5 Function.prototype.bind方法。 To put it simply, you can bind a method's this keyword. 简单地说,您可以绑定方法的this关键字。 Follow the link or use a search engine for more details . 请点击链接或使用搜索引擎获取更多详细信息 If you use this method, beware that it is not compatible with older browsers, but the provided link shows an alternate implementation you may use to implement the .bind method in older browsers. 如果您使用此方法,请注意它与旧版浏览器不兼容,但提供的链接显示了您可能用于在旧版浏览器中实现.bind方法的替代实现。

var myObj = new function(){
    this.theValue = "The rain in Spain";
    this.functionA = function() {
        alert(this.theValue);
    }.bind(this);
    this.moreFunctions = {
        functionB: function() {
            alert(this.theValue);
        }.bind(this)
    };
}();

myObj.functionA();
myObj.moreFunctions.functionB();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM