对象内部的 JavaScript 闭包:什么别名应该有“this”?用于传递给匿名函数?

发布于 2024-09-07 04:38:40 字数 408 浏览 5 评论 0原文

我们与我们的团队讨论下一步: 正确的名称应该有变量,该变量将是匿名函数中“this”的别名。简单的例子:

var SomeConstructor = function() {

    this.someProperty = 'bingo';

    this.someMethod = function() {

        var myObjectAlias = this;

        $('a').click(function() {
            alert( myObjectAlias.someProperty );                  
        });

    }
}

所以我的问题是 - 什么正确的名称应该有变量“myObjectAlias”?或者您的代码中使用了什么?

We have discussion with our team about next:
What correct name should have variable which will be alias for "this" in anonymous function. Simple example:

var SomeConstructor = function() {

    this.someProperty = 'bingo';

    this.someMethod = function() {

        var myObjectAlias = this;

        $('a').click(function() {
            alert( myObjectAlias.someProperty );                  
        });

    }
}

So my question is - What correct name should have variable "myObjectAlias"? Or what used in your code for example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

失而复得 2024-09-14 04:38:40

您会看到很多代码示例。

var that = this;

$('a').click(function() {
         alert( that.someProperty );                  
});

这样做;其实没关系,你想要什么就什么。

You see a lot of in code samples.

var that = this;

$('a').click(function() {
         alert( that.someProperty );                  
});

To do that; it really doesn't matter, whatever you want.

素手挽清风 2024-09-14 04:38:40

在这些情况下,我倾向于使用 self,或者明确地使用 _self,具有 this含义,而无需冲突,让事情简单直观,至少对我来说是这样。

例如:

self.property
self.doSomething()
if(self.hasPickles) {
  self.eat();
}

这当然是一个偏好问题,但我所交谈过的大多数 JS 开发人员都认为这种命名风格非常直观,这并不是说大多数人都这么认为,但事实并非如此没关系吧?做对你的团队有用的事情:)

I tend to use self in these cases, or _self to be explicit, has the meaning of this without the conflicts, keeping things simple and intuitive, at least for me.

For example:

self.property
self.doSomething()
if(self.hasPickles) {
  self.eat();
}

It's all a matter of preference of course, but the majority of JS developers that I've talked to find this naming style pretty intuitive, that's not to say the overall majority does, but that doesn't matter does it? Do what works for your team :)

公布 2024-09-14 04:38:40

我认为有时使用有意义的名称是件好事,例如“表单”或“容器”或其他名称。简而言之,小代码块并不是什么大问题,但在表单或对话框的复杂初始化中,往往会出现几个不同的“有趣”对象。

I think sometimes it's good to use meaningful names, like "form" or "container" or whatever. In short little blocks of code is not that big a deal, but in complicated initializations for forms or dialogs there tends to be several different "interesting" objects buzzing around.

半山落雨半山空 2024-09-14 04:38:40

我更喜欢使用 self ,原因是 Nick Craver 描述的。

I prefer to use self for reasons Nick Craver described.

听你说爱我 2024-09-14 04:38:40

切线:考虑为构造函数使用原型:

var SomeConstructor = function() {}
SomeConstructor.prototype = {

    someProperty: 'bingo',

    someMethod: function() {   
        var self = this;    
        $('a').click(function() {
            alert( self.someProperty );                  
        });    
    }
}

someProperty 和 someMethod 属性可以在所有实例之间共享,因此它们可以通过这种方式正常工作。每当您需要为每个实例提供单独的值时,您应该将它们放在构造函数本身中。

on a tangent: consider using prototypes for your constructor:

var SomeConstructor = function() {}
SomeConstructor.prototype = {

    someProperty: 'bingo',

    someMethod: function() {   
        var self = this;    
        $('a').click(function() {
            alert( self.someProperty );                  
        });    
    }
}

The someProperty and someMethod properties can be shared among all instances, so they work fine this way. Whenever you need separate values for each instance, you should put those in the constructor itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文