- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 JavaScript 的新手,我正在清理我下载的一些 JavaScript 代码。此代码中的不一致之处之一是 this
和引用它的局部变量 that
的混合使用。
示例代码片段(jQuery UI 小部件中的私有(private)方法):
_populateLists: function(options) {
//do stuff with this
var that = this;
options.each(function(index) {
//use both this AND that, they are different in this scope
});
//here this and that are the same thing
//I want to be consistent, is one preferable over the other?
},
在整个代码的许多地方,范围是这样的 this === that
,即使在同一行代码中,也存在混合用法。
为了可读性和可维护性,使用this
还是that
更好?
注意:我意识到很多这类事情取决于开发人员的偏好。但在我决定重写代码以使用其中一个之前,我想了解任何推理是否/为什么一个比另一个更受欢迎。
编辑: 在这个脚本中,我认为 唯一 this
被分配给局部变量的原因是它可以被引用从内部闭包内。
最佳答案
this
的值通常分配给局部变量的原因是您可以关闭该值并在嵌套函数中使用它。
this
是一个特殊变量,与普通局部变量有些不同,因为它会自动设置为调用函数的对象(如果有);否则,全局对象。然而,this
的这种内在值(value)由于 jQuery 对 call
的自由使用而有些困惑。和 apply
,它允许调用者指定 this
的值。
在嵌套函数中,this
不继承其父级的 this
的值,就像它通过作用域链继承父级的局部变量一样。
因此,如果我们在嵌套函数中需要它,我们必须将 this
的值存储在局部变量中,例如您示例中的 each
回调.
var a = { fn: function() {
// here, `this` is the object `a`
var x = this;
function b() {
// here, `this` is `window` because `b` is invoked with no context
// ...but we still have access to `x`, whose value would be `a`
}
b();
}};
a.fn(); // by invoking `fn` as a property of `a`, we implicitly set `fn`'s
// `this` to `a`.
// Compare to:
var f = a.fn;
f(); // we now invoke `fn` with no context, so its `this` will be `window`.
a.fn.call(window); // or by specifying context explicitly
当然,当您仍在父作用域中时,this
仍将等于 that
(或 self
或其他)。乍一看,两者似乎没有区别,但有一个重要的性能影响:
缩小。如果您将this
分配给局部变量,则对该变量的所有引用都可以减少为一个字符。不能引用 this
。比较这个缩小的输出:
function a() {this.w=1;this.x=2;this.y=3;this.z=4;}
function b() {var t=this;t.w=1;t.x=2;t.y=3;t.z=4;}
通过对 this
的 4 次引用,您可以使用变量保存字节。如果无论如何都必须为内部函数捕获 this
,在外部函数中使用局部变量而不是 this
可以节省成本,即使只有一个引用也是如此。
关于javascript - 最好使用 "this"还是 "that"(其中 var that = this)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14046682/
这个问题已经有答案了: Can I bind an array to an IN() condition in a PDO query? (23 个回答) 已关闭 5 年前。 任何人都可以看到我如何在
我阅读了关于此的 bash 手册页,但我不明白其中的区别。我对它们进行了测试,它们似乎产生了完全相同的结果。 如果值不是通过命令行参数设置的,我想设置一个变量的默认值。 #!/bin/bash var
我为我的网站开了一家商店,并让它运行起来,但我意识到它无法正确购买商品。换句话说,您不会走进一家商店,拿起一件商品,购买,再次购买,购买,再次购买,等等,以获得想要的数量。你一次捕获他们。我的网站缺少
基本上,我想知道为什么会这样(将列表的内存地址作为参数传递): void init_lista (elemPtr *list) { *list = NULL; } int main(){
看到这个问题:Is there a (built-in) way in JavaScript to check if a string is a valid number?还有这个:jsperf ,其
我有以下字符串: 我想用正则表达式替换所有后面有 px 的数字,并用 X 乘以它们的值。 (X 是一个变量)。 所以如果X=3,结果就是 请注意 X 必须是我将检索到函数的变量 最佳答案 以下代码
这个问题在这里已经有了答案: 关闭 13 年前。 同时 (var != var) System.out.println("循环.."); 执行它..如何声明..var
我只是好奇。我想知道表达式是否有特定原因 var &= expr 行为方式与不同 var = var && expr. 看起来第一个表达式中的表达式正在执行,而不管 var 上的 false 值。 我
我有这个 Ruby 代码: var1 = 10 puts var1.object_id var1 = var1 + 0 puts var1.object_id var1 = var1 + 1 puts
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: demote boost::function to a plain function pointer 所以我
好吧,堆栈溢出, 我花了几个小时来解决 javascript 中的问题(在 NodeJS 服务器中编写),但我真的不明白。 这就是发生的事情: var data = {x: 50}; var temp
首先,我在这里处理 1 和 0,我很清楚 1 == true 和 0 == false。但是我很好奇为什么这不起作用: $var = 1; echo $var; /* 1 */ $var = $var
标题说的是什么:将变量封装在 {}、"" 或 "{}"中是什么意思?我无法在网上找到关于此的任何解释 - 除了使用不会产生任何结果的符号外,我无法引用它们。 这是一个例子: declare -a gr
我需要将信息发送到我的 Html。例如 $(document).ready(function() { var = "'#"+result.tag+"'" // var = '#tag_dinamy
是否可能,如果可以,如何将以下表达式转换为一行? DEV=$(lsblk -no KNAME,MODEL | grep 'ModelNAME') DEV=${DEV%%'ModelNAME'} 简单的
isset($var) == "Test" 和 isset($var) && $var == 'Test" 有什么区别? 最佳答案 这里是一个简短的例子: $var = "Chuck Test"; v
isset($var) == "Test" 和 isset($var) && $var == 'Test" 有什么区别? 最佳答案 这里是一个简短的例子: $var = "Chuck Test"; v
如果我有一个字符串:[Object a:var b:var c:var]; 如何将 a:、b: 和 c: 与正则表达式匹配? 我试过:\[.+\s+(.+:).+\] 但它不适用于 a、b 和 c,它
这个问题在这里已经有了答案: Why does this if-statement combining assignment and an equality check return true? (
我正在 Powershell 中使用 SCSM,但遇到了 if 语句的问题。 我有一个函数,它根据作为变量传递给函数的条件收集数据。 例子: $JMLs1 = collectTickets -crit
我是一名优秀的程序员,十分优秀!