gpt4 book ai didi

javascript - 坏的擒纵系统和未闭合的弦

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:50:30 26 4
gpt4 key购买 nike

var freebie = ' \
<div class="item'+last+'" data-type="psd" data-visited="false"> \
<div class="baseBubble" /> \
<div class="baseStroke" /> \
<div class="baseThickerStroke" /> \
<div class="thumbnail"> \
<div class="overlay" /> \
<img src="'+item['thumb'][0]+'" width="'+item['thumb'][1]+'" height="'+item['thumb'][2]+'" /> \
</div> \
</div>';

有谁知道为什么这会在 JSLint 中返回 2 个错误?

这是 2 个错误的图片:

enter image description here

最佳答案

简短的回答,您需要配置 JSLint 以容忍 ECMAScript 5。

这可以通过以下方式完成:

/*jslint es5: true, all-your-other-jslint-options */

长答案:

序列 \ 后跟一个行终止符,直到最近(第 5 版,2009 年 12 月)才真正有效的 Javascript。

根据 ECMAScript (ECMA-262) 标准的上一个迭代(版本 3),第 7.8.4 节指出:(删除了一些不相关的条目):

StringLiteral ::    " DoubleStringCharacters(opt) "    ' SingleStringCharacters(opt) 'SingleStringCharacters ::    SingleStringCharacter SingleStringCharacters(opt)SingleStringCharacter ::    SourceCharacter but not single-quote ' or backslash \ or LineTerminator    \ EscapeSequence

所以您的序列在上面的最后一行结束,一个 \ 后跟语法元素 EscapeSequence。进一步检查:

EscapeSequence ::    CharacterEscapeSequence    0 [lookahead ∉ DecimalDigit]    HexEscapeSequence    UnicodeEscapeSequenceCharacterEscapeSequence ::    SingleEscapeCharacter    NonEscapeCharacterSingleEscapeCharacter :: one of    ' " \ b f n r t vNonEscapeCharacter ::    SourceCharacter but not EscapeCharacter or LineTerminatorEscapeCharacter ::    SingleEscapeCharacter    DecimalDigit    x    uHexEscapeSequence ::    x HexDigit HexDigitUnicodeEscapeSequence ::    u HexDigit HexDigit HexDigit HexDigit 

由于 \ 之后的下一个字符既不是 0x 也不是 u,唯一的选择是 CharacterEscapeSequence 归结为 SingleEscapeCharacter(不是这种情况,因为行终止符不是列出的字符之一)或 NonEscapeCharacter(明确 排除行终止符的可能性)。

该部分底部还有此注释:

NOTE: A 'LineTerminator' character cannot appear in a string literal, even if preceded by a backslash . The correct way to cause a line terminator character to be part of the string value of a string literal is to use an escape sequence such as \n or \u000A.


现在,ECMAScript 5 改变了一点点。从那里开始,他们修改了 SingleStringCharacter 的定义:

SingleStringCharacter ::    SourceCharacter but not one of ' or \ or LineTerminator    \ EscapeSequence     LineContinuationLineContinuation ::    \ LineTerminatorSequence

并将注释修改为:

NOTE: A line terminator character cannot appear in a string literal, except as part of a LineContinuation to produce the empty character sequence. The correct way to cause a line terminator character to be part of the String value of a string literal is to use an escape sequence such as \n or \u000A.

而且,作者并没有为所有当前脚本破坏 JSLint,而是明智地决定让 ECMAScript 5 支持成为可选的,需要更改 JSLint 选项才能激活它。这样,如果您明确告诉它,它将只允许 ECMAScript 5。

您可以访问http://www.jslint.com/网站并确认:

Code:    var xyzzy = ' \    hello";Error:    Problem at line 1 character 16: This is an ES5 feature.var xyzzy = ' \    Problem at line 2 character 13: Unclosed string.hello";    Problem at line 2 character 13: Stopping. (66% scanned).

如果您随后向下滚动到标志部分,则会有一个用于Tolerate ES5 syntax 的条目,设置后将删除该错误。

关于javascript - 坏的擒纵系统和未闭合的弦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8090062/

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