出现意外的“。” 来自jslint(http://jslint.com/)的以下代码:
function test(foo) {
"use strict";
return (foo || "").replace("bar", "baz");
}
jslint为什么||有问题 运算符强制为空字符串,以便在foo作为undefined传入的情况下可以执行替换操作而不会导致错误?
通过:
function test(foo) {
"use strict";
var xFoo = (foo || "");
return xFoo.replace("bar", "baz");
}
我知道它是基于观点的,我可以忽略它,等等...但是试图理解为什么这样的链接不被接受。也了解eshint,但我并不是想绕开此消息,只是想了解原因。
似乎第一种方法更加简洁明了,因为它不需要额外的变量(xFoo)。
在所有条件下,这两种功能的作用完全相同。
使用String()
构造 函数可消除jslint的错误
function test(foo) {
"use strict";
return String(foo || "").replace("bar", "baz");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句