简体   繁体   English

为什么要将此分配给另一个变量?

[英]why would you assign this to another variable?

At the start of the source code for underscore.js , you see this: underscore.js的源代码开头 ,您会看到以下内容:

var root = this;

var previousUnderscore = root._;

So the question becomes why didnt the author just write: 所以问题就变成了为什么作者不写:

var previousUnderscore = this._;

Two reasons, 有两个原因,

  1. Readability, it makes it clearer what the code is doing, what it is referencing. 可读性,它使代码正在做什么,正在引用什么变得更加清晰。 this is very opaque. 这是非常不透明的。
  2. depending on context, this can refer to lots and lots and lots of differnt objects, root may be used in multiple place and will always refer to the correct object. 根据上下文的不同,它可以引用很多很多不同的对象,root可以在多个地方使用,并且始终引用正确的对象。

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

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