开发者

JavaScript Heredoc? Escape Newline: JSLint Bad Escapement?

var wrapped_string = "shouldn't this\
work?"

JSLint gives a bad es开发者_JAVA技巧capement error.

How do we do heredoc in JavaScript then? I have a long HTML string.


I think the reason about why this is treated as an error is because this syntax extension was recently standardized on the ECMAScript 5th Edition Specification.

Although is widely supported, on an ECMAScript 3 implementation (which is the most widely implementation available) you don't have any guarantee that it will work, or just cause an early SyntaxError exception.

Another reason some people argue to avoid it, is that if you have any other character that isn't a LineTerminator you will have a SyntaxError, even on an ECMAScript 5 implementation.

For example, if you had a space after the backslash, the code will produce an error, and is not easy to spot just by reading the code:

// works:
var str = "foo\
bar";

// an space after it, SyntaxError:
var str = "foo\ 
bar";


JSLint hurts your feelings, nothing else, try and running it on jQuery to see that.

The escapement is fine when you have a ton of strings, I consider it cleaner than using a ton of +.


You can enable ECMA5 syntax via jslint.yml. This will all escaping newlines.

es5: true # true if ECMAScript 5 syntax should be allowed

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜