简体   繁体   English

javascript 这是什么? 这是不好的做法吗?

[英]What this in javascript? Is it bad practice?

Right now i'm following a tutorial, and the guy who does the tutorial does this现在我正在学习一个教程,做这个教程的人就是这样做的

let ball = ballObject = new AmmobtTransform(); 

inside a function to make "ball" a local variable, and "ballObject" a global variable at the same time.在 function 中使“ball”成为局部变量,同时使“ballObject”成为全局变量。 Now, he calls the "= ballObject" bit a 'handler', but I cant seem to find documentation of that (i find JavaScript event handlers eg touchdown events instead) so what is this, and is it an old/new feature?现在,他将“= ballObject”位称为“处理程序”,但我似乎找不到相关文档(我找到 JavaScript 事件处理程序,例如触地事件),这是什么,它是旧/新功能吗? A link to documentation would be fine.一个文档链接就可以了。 Thank you.谢谢你。

It's a chained assignment.这是一个链式作业。 An assignment expression ( a = b ) has a result, which is the value that was assigned.赋值表达式 ( a = b ) 有一个结果,即被赋值的值。 So by doing let ball = ballObject = new AmmobtTransform();所以通过let ball = ballObject = new AmmobtTransform(); the author is doing (effectively¹) this:作者正在(有效地¹)这样做:

ballObject = new AmmobtTransform();
let ball = ballObject;

It's something that's reasonable to do in some situations.在某些情况下这样做是合理的。 Like anything, it can also be misused.像任何东西一样,它也可能被滥用。 But:但:

...to make "ball" a local variable, and "ballObject" a global variable at the same time... ...使“ball”成为局部变量,同时使“ballObject”成为全局变量...

If you mean that ballObject is not declared anywhere, and the author is using that statement to create a new global variable, rather than just assigning to a variable that is already declared in some containing scope, that is definitely poor practice.如果您的意思是ballObject没有在任何地方声明,并且作者正在使用该语句创建一个的全局变量,而不是仅仅分配给已经在某些包含 scope 的变量中声明的变量,那绝对是不好的做法。 It relies on what I call The Horror of Implicit Globals .它依赖于我所说的隐式全局的恐怖 In general, use strict mode so that doing that is the error it always should have been.一般来说,使用严格模式,这样就应该一直这样做。 :-) :-)

...he calls the "= ballObject" bit a 'handler'... ...他将“= ballObject”位称为“处理程序”...

That seems odd.这似乎很奇怪。 The constructor name AmmobtTransform doens't immediately suggest to me that it's any kind of "handler," and if it were, it wouldn't just apply to ballObject , but to ball as well.构造函数名称AmmobtTransform不会立即向我暗示它是任何类型的“处理程序”,如果是,它不仅适用于ballObject ,也适用于ball

To sum up:总结一下:

  1. It's a chained assignment ( a = b = c )这是一个链式分配( a = b = c
  2. Chained assignments are occasionally appropriate链式分配有时是合适的
  3. Relying on The Horror of Implicit Globals is never appropriate;依赖隐式全局的恐怖是不合适的; use strict mode to avoid doing it by accident使用严格模式避免意外
  4. Calling ballobject a "handler" seems odd ballobject称为“处理程序”似乎很奇怪

¹ Okay, so it's really a bit more like: ¹ 好的,所以它真的有点像:

let ball;
const tmp =  new AmmobtTransform();
ballObject = tmp;
ball = tmp;

...(with tmp under the covers and gone by the end of it) but that's just pedantry on my part. ...(在封面下使用tmp并在它结束时消失了)但这只是我的迂腐。 ;-) ;-)

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

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