简体   繁体   English

在JavaScript对象文字中,我应该指代“ this”还是变量名?

[英]In a JavaScript object literal, should i refer to 'this' or the variable name?

I was having issues in my object literal using this , but when changing to the name of the object variable itself, my problems went. 我在使用this对象字面量中遇到问题,但是当更改为对象变量本身的名称时,我的问题就解决了。

When using this, I was getting a 'myFunction' is not a function error. 使用此功能时,出现“ myFunction”不是函数错误。

Should I be doing this: 我应该这样做吗:

var app = {

    init: function() {

        this.myFunction();

    },

    myFunction: function() {

        return "Hello world!";

    }

};

Or, this? 或这个?

var app = {

    init: function() {

        app.myFunction();

    },

    myFunction: function() {

        return "Hello world!";

    }

};

What is the correct way to do this? 正确的方法是什么?

This particular example is a very simplified version of my problem and may not reproduce the same issues, but it's the same general pattern I was following, where functions would call each other using this . 这个特定的示例是我的问题的非常简化的版本,可能不会重现相同的问题,但是它与我遵循的一般模式相同,其中函数将使用this相互调用。

Thanks, Michael. 谢谢,迈克尔。

What is the correct way to do this? 正确的方法是什么?

Both are correct, depending on what you're trying to achieve. 两者都是正确的,具体取决于您要实现的目标。

If you're only ever going to have one of these and you're not going to copy those methods to any other objects (which you can do in JavaScript), then using app probably makes more sense. 如果您只打算使用这些方法之一,而又不想将这些方法复制到任何其他对象(可以在JavaScript中完成),那么使用app可能更有意义。

My guess is that the problem you were having when you were using this was that you'd done something like this: 我的猜测是,使用this时遇到的问题是您做了这样的事情:

somethingThatAcceptsACallback(app.init);

or 要么

someElement.onclick = app.init;

...and when init was called, it couldn't call myFunction . ...并且在调用init时,无法调用myFunction That's because in JavaScript (for now), the way this is set mostly depends on how a function is called , not where the function is defined. 这是因为在JavaScript(现在),一路this设置主要取决于函数是如何调用 ,而不是其中定义的功能。 In both of the above cases, although a reference to the function is given to the other code, when the other code calls it, it doesn't do anything to set this to app . 在以上两种情况下,尽管对该函数的引用都被另一个代码引用,但是当另一个代码调用它时,它并没有做任何事情this设置为app

You can use ES5's Function#bind (which can be shimmed on IE8 and older) to create functions that, when called, call the original function with the correct this value: 您可以使用ES5的Function#bind (可以在IE8及更高版本上填充)来创建函数,这些函数在被调用时以正确的this值调用原始函数:

somethingThatAcceptsACallback(app.init.bind(app));

or 要么

someElement.onclick = app.init.bind(app);

But again, if app is a one-off, just using app within your functions is fine. 但是,如果app是一次性的,则只需在您的函数中使用app就可以了。

More about this (on my blog) : 关于this更多信息(在我的博客上)

in your first code this refers and app refers same. 在您的第一个代码中, 引用与应用程序引用相同。 In my point of view I can suggest 我认为我可以建议

var app = {    
    init: function() {    
        app.myFunction();   
    },   
    myFunction: function() {    
        return "Hello world!";    
    }   
}; 

this code for better understanding. 这段代码可以更好地理解。

In javascript this changes depending on how the function is called. 在javascript中, this变化取决于函数的调用方式。 This problem is often gotten around by creating a variable named self which is assigned the value of this at a stage when you know positively the value of this 这个问题通常是通过创建一个命名的变量得到解决, self其被指派值this的阶段,当你知道的肯定值this

var app = {
    self: this,
    init: function() {

        self.myFunction();

    },
    myFunction: function() {
        return "Hello world!";
    }
};

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

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