有一个参考,以便轻松确定好的或坏的想法吗?

时间:2021-06-15 21:01:48

I sometimes assign the this pointer to a var. The benefit of it is that I don't loose the scope in anonymous callback functions. Is that a good or bad idea?

我有时将this指针指向var。它的好处是我没有放弃匿名回调函数的范围。这是一个好主意还是坏主意?

Example:

var Foo = (function($) {
  var Foo = function() {
    var self;

    self = this;

    this.init = function() {
      $('#foo').click(function(e) {
        self.onClick();
      });
    };

    this.onClick = function() {
      console.log(this);
    };

    this.init();
  };

  return function() {
    return new Foo();
  };
})(jQuery);

Thank you!
{Jim}

谢谢! {}吉姆

5 个解决方案

#1


3  

If you need a reference to the this value of a containing scope (which is certainly a reasonable requirement), it must be a good idea, particularly in cases where you are not in control of the function call (such as event handler functions) where it's your only option.

如果你需要引用包含范围的这个值(这当然是一个合理的要求),那一定是个好主意,特别是在你不能控制函数调用的情况下(比如事件处理函数)这是你唯一的选择。

#2


1  

It's not a bad idea. Whether or not it is a good idea is mostly stylistic.

这不是一个坏主意。这是否是一个好主意大多是风格。

An alternate way to write your code is to hold a reference to the onClick function in the closure. However, this won't be bound for the onClick function when calling it this way. For example:

编写代码的另一种方法是在闭包中保存对onClick函数的引用。但是,当以这种方式调用时,这不会受到onClick函数的约束。例如:

var Foo = (function($) {
  var Foo = function() {
    this.init = function() {
      $('#foo').click(function(e) {
        onClick();
      });
    };

    var self = this;  // Or use ES5's Function.prototype.bind

    var onClick = this.onClick = function() {
      console.log(self);
    };

    this.init();
  };

  return function() {
    return new Foo();
  };
})(jQuery);

I prefer this style to using self (or that or whatever name you chose) if the object's methods are all in a closure (as in your case).

如果对象的方法都在闭包中(如你的情况),我更喜欢这种风格使用self(或你选择的那个或任何名字)。

#3


1  

There's nothing wrong with doing it that way, except perhaps that you'd have to come up with more synonyms for "this" and "self" if your scope nesting gets deeper than one level. My preferred method for hanging onto a particular "this" value is to use Function.prototype.bind, which is part of the ECMAScript 5 spec and being implemented in browsers now. As of this moment, only Google Chrome supports it natively, to my knowledge, but it's easy to stick it in with a polyfill, as I've been doing for some time.

这样做是没有错的,除非您的范围嵌套深度超过一个级别,否则您必须为“this”和“self”提供更多同义词。我挂在特定“this”值上的首选方法是使用Function.prototype.bind,它是ECMAScript 5规范的一部分,现在在浏览器中实现。截至目前,根据我的知识,只有谷歌Chrome本身支持它,但很容易将其粘贴到polyfill中,就像我已经做了一段时间一样。

#4


1  

It's neither a good nor a bad idea. It's your only chance to keep a reference to an outer scope/context.

这既不好也不坏。这是您保留对外部范围/上下文的引用的唯一机会。


I was asked for more detail, here we go:

我被问到更多细节,我们走了:

A this value is a special object which is related with the execution context. Therefore, you could say it is a context object (an object in which context the execution context is activated).

此值是与执行上下文相关的特殊对象。因此,您可以说它是一个上下文对象(一个在其中激活执行上下文的上下文的对象)。

  • In the global context, this is the global object itself.
  • 在全球范围内,这是全球对象本身。

  • In case of a function context, this in every single function call may be different. this is determined on entering the context, and in case with a function code the value can be absolutely different every time.
  • 在函数上下文的情况下,每个函数调用中的这可能是不同的。这是在进入上下文时确定的,并且在功能代码的情况下,每次都可以使值完全不同。

And even this that is a very brief summary of the whole topic. But you hopefully understand why you need to store a reference for the this value to access it from another function context.

甚至这是对整个主题的一个非常简短的总结。但是,您希望了解为什么需要存储此值的引用以从另一个函数上下文访问它。

#5


0  

It seems like the only way to retain a reference to the scope. But don't do this (from your example):

这似乎是保留对范围的引用的唯一方法。但是不要这样做(从你的例子):

var self;
self = this;

Instead combine them into one line:

而是将它们组合成一行:

var self = this;

#1


3  

If you need a reference to the this value of a containing scope (which is certainly a reasonable requirement), it must be a good idea, particularly in cases where you are not in control of the function call (such as event handler functions) where it's your only option.

如果你需要引用包含范围的这个值(这当然是一个合理的要求),那一定是个好主意,特别是在你不能控制函数调用的情况下(比如事件处理函数)这是你唯一的选择。

#2


1  

It's not a bad idea. Whether or not it is a good idea is mostly stylistic.

这不是一个坏主意。这是否是一个好主意大多是风格。

An alternate way to write your code is to hold a reference to the onClick function in the closure. However, this won't be bound for the onClick function when calling it this way. For example:

编写代码的另一种方法是在闭包中保存对onClick函数的引用。但是,当以这种方式调用时,这不会受到onClick函数的约束。例如:

var Foo = (function($) {
  var Foo = function() {
    this.init = function() {
      $('#foo').click(function(e) {
        onClick();
      });
    };

    var self = this;  // Or use ES5's Function.prototype.bind

    var onClick = this.onClick = function() {
      console.log(self);
    };

    this.init();
  };

  return function() {
    return new Foo();
  };
})(jQuery);

I prefer this style to using self (or that or whatever name you chose) if the object's methods are all in a closure (as in your case).

如果对象的方法都在闭包中(如你的情况),我更喜欢这种风格使用self(或你选择的那个或任何名字)。

#3


1  

There's nothing wrong with doing it that way, except perhaps that you'd have to come up with more synonyms for "this" and "self" if your scope nesting gets deeper than one level. My preferred method for hanging onto a particular "this" value is to use Function.prototype.bind, which is part of the ECMAScript 5 spec and being implemented in browsers now. As of this moment, only Google Chrome supports it natively, to my knowledge, but it's easy to stick it in with a polyfill, as I've been doing for some time.

这样做是没有错的,除非您的范围嵌套深度超过一个级别,否则您必须为“this”和“self”提供更多同义词。我挂在特定“this”值上的首选方法是使用Function.prototype.bind,它是ECMAScript 5规范的一部分,现在在浏览器中实现。截至目前,根据我的知识,只有谷歌Chrome本身支持它,但很容易将其粘贴到polyfill中,就像我已经做了一段时间一样。

#4


1  

It's neither a good nor a bad idea. It's your only chance to keep a reference to an outer scope/context.

这既不好也不坏。这是您保留对外部范围/上下文的引用的唯一机会。


I was asked for more detail, here we go:

我被问到更多细节,我们走了:

A this value is a special object which is related with the execution context. Therefore, you could say it is a context object (an object in which context the execution context is activated).

此值是与执行上下文相关的特殊对象。因此,您可以说它是一个上下文对象(一个在其中激活执行上下文的上下文的对象)。

  • In the global context, this is the global object itself.
  • 在全球范围内,这是全球对象本身。

  • In case of a function context, this in every single function call may be different. this is determined on entering the context, and in case with a function code the value can be absolutely different every time.
  • 在函数上下文的情况下,每个函数调用中的这可能是不同的。这是在进入上下文时确定的,并且在功能代码的情况下,每次都可以使值完全不同。

And even this that is a very brief summary of the whole topic. But you hopefully understand why you need to store a reference for the this value to access it from another function context.

甚至这是对整个主题的一个非常简短的总结。但是,您希望了解为什么需要存储此值的引用以从另一个函数上下文访问它。

#5


0  

It seems like the only way to retain a reference to the scope. But don't do this (from your example):

这似乎是保留对范围的引用的唯一方法。但是不要这样做(从你的例子):

var self;
self = this;

Instead combine them into one line:

而是将它们组合成一行:

var self = this;