var self = this;糟糕的模式? [英] Is var self = this; a bad pattern?

查看:98
本文介绍了var self = this;糟糕的模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己需要:

var self = this;

在我的javascript'类'中很多。尽管通常这样做,但感觉有点不对劲。
我希望在这个问题中找到的是一个更好的方法来解决这个问题,或者说服一些东西来说服这个问题很好。

a lot within my javascript 'classes'. Although this is commonly done, it feels a bit wrong. What I'm hoping to find in this question is a better way to deal with this, or a something to convince me this is quite alright.

是这是保持正确绑定的标准方法吗?我应该在任何地方标准化使用'self',除非我明确需要'this'。

Is this the standard way to keep the correct bindings around? Should I standardize on using 'self' everywhere, unless i explicitly need 'this'.

编辑:我确切地知道为什么我需要这个,我只是想知道它是否被认为有点邪恶,为什么。我知道在调用方法时还有'apply'内置的javascript函数来显式定义范围。是否更好?

edit: I know exactly why I need this, I'm just wondering if it's considered a bit evil and why. I'm aware there's also the 'apply' built-in javascript function to explicitly define scope when calling a method. Is it better?

推荐答案

正如其他人所说:这个额外变量是(在某种程度上)获得的唯一途径关于这个是一个特殊表达式的事实,因此,它不是一个变量,不会被绑定在执行上下文/闭包中。

As others have said: This "extra variable" is (at some level) the only way to get about the fact that this is a special expression and thus, being not a variable, is not bound in an execution context/closure.

然而,我认为你要问的(或我真正想要回答的)是:

However, what I think you are asking (or what I really want to answer) is:



是否应该在每个方法/构造函数的顶部放置 var self = this




摘要



虽然我尝试了一次,并且有同样的问题,但我不再使用这种方法。现在我保留构造,以便在需要访问闭包时。对我而言,它增加了一点嘿,这就是我真正想要的!我的代码语义:

Summary

While I tried this once, and had the same question, I no longer use this approach. Now I reserve the construct for when I need access in a closure. To me it adds a little "hey, this is what I really want!" semantic to my code:

this - >这个自我 - >这个(但确实如此)在一个闭包中



...虽然通常这样做,但感觉有点不对劲。我希望在这个问题中找到的是一个更好的方法来解决这个问题,或者说服我这样做的事情很好。

...Although this is commonly done, it feels a bit wrong. What I'm hoping to find in this question is a better way to deal with this, or a something to convince me this is quite alright.


做对你感觉合适的事。不要害怕尝试一种方法并稍后再切换(但请尽量在每个项目中保持一致: - )

Do what feels right to you. Don't be afraid to try one method and switch back later (but please try to remain consistent within each project :-)



这是保持正确绑定的标准方法吗?我应该在任何地方标准化使用'self',除非我明确需要'this'。

Is this the standard way to keep the correct bindings around? Should I standardize on using 'self' everywhere, unless i explicitly need 'this'.


self是最常用的名字。如上所述,我更喜欢相反的方法 - 使用这个除非需要闭包绑定。

"self" is the most common name used. As per above, I prefer the opposite approach -- to use this except when a closure binding is required.



..如果它被认为有点邪恶,为什么。

..if it's considered a bit evil and why.


邪恶是一个愚蠢的主观术语(虽然有时很有趣)。我从来没有说过它是邪恶的,只是为什么我不遵循这个方法。有些人告诉我,因为不使用分号,我是邪恶的。我告诉他们他们实际上应该提出好的论点和/或更好地学习JavaScript: - )

Evil is a silly subjective term (albeit fun sometimes). I've never said it was evil, just why I do not follow the approach. Some people tell me I am "evil" for not using semi-colons. I tell them they should actually come up with good arguments and/or learn JavaScript better :-)



I我知道在调用方法时还有'apply'内置的javascript函数来显式定义范围。它更好吗?

I'm aware there's also the 'apply' built-in javascript function to explicitly define scope when calling a method. Is it better?


的问题应用/调用是你必须在函数调用点使用它们。如果某人 else 调用您的某个方法,那将无济于事,因为可能已经关闭。这对于jQuery样式的回调最有用,其中 this 是回调的元素/项目等。

The problem with apply/call is that you must use them at point of the function invocation. It won't help if someone else calls one of your methods as the this may already be off. It's most useful for doing things like the jQuery-style callbacks where the this is the element/item of the callback, etc.

喜欢避免会员上的需要自我,因此通常会将所有会员功能提升为接收者( this )只是流过的属性,通常是按预期。

I like to avoid "needing self" on members and thus generally promote all member functions to properties where the receiver (this) just "flows through", which is normally "as expected".

我的代码中的私有方法以_开头,如果用户调用它们,那就是它们。当使用原型方法创建对象时,这也可以更好地工作(非常需要)。但是,道格拉斯·克罗克福德不同意我的这种私人做法,有些情况下查找链可能会通过注入一个意外的接收者来阻止你:

The "private" methods in my code begin with a "_" and if the user calls them, that's on them. This also works better (is required, really) when using the prototype approach to object creation. However, Douglas Crockford disagrees with this "private" approach of mine and there are some cases where the look-up chain may thwart you by injecting an unexpected receiver:

在构造函数中使用self绑定也会锁定上限方法的查找链 (它不再是多态的!),这可能是也可能不正确。我认为这通常是不正确的。

Using the "self" bound in the constructor also locks the upper limit of the look-up chain for a method (it is no longer polymorphic upward!) which may or may not be correct. I think it's normally incorrect.

快乐编码。

这篇关于var self = this;糟糕的模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆