gpt4 book ai didi

javascript - JSLint - 预期为 '{' 而看到 'type'

转载 作者:行者123 更新时间:2023-11-28 14:45:50 25 4
gpt4 key购买 nike

JSLint 不断返回以下错误:预期 '{' 而看到 'type',我该如何修复它?

var pfx = ['webkit', 'moz', 'MS', 'o', '']; 
function prefixedEventListener(element, type, callback) {
for (var p = 0; p < pfx.length; p++) {
if (!pfx[p]) type = type.toLowerCase();
element.addEventListener(pfx[p]+type, callback, false);
}
}

最佳答案

我认为根据 JSlint 规则,if 应该有大括号

if (!pfx[p]) {
type = type.toLowerCase();
}

来自 JSLint 文档,

block ( http://www.jslint.com/help.html )

JSLint 需要带有 functionifswitchwhilefor 的 block >、dotry 语句,没有其他地方。

JSLint 预计 ifwhiledofor 语句将使用 block { 即用大括号括起来的语句}

JavaScript 允许这样编写 if:

if (condition)
statement;

众所周知,这种形式会在许多程序员处理相同代码的项目中导致错误。这就是 JSLint 期望使用 block 的原因:

if (condition) {
statements;
}

经验表明,这种形式更有弹性。

关于javascript - JSLint - 预期为 '{' 而看到 'type',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46378872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com