One of my colleagues used backslashes when setting the string value of a property in a JS object:
shareButtonsHtml : '\
<span class="share-fb">\
<a href="#" class="fb-share"><span class="fb-share-icon"></span>Share</a>\
<span class="fb-share-count"><i></i><u></u>0</span>\
</span>\
<div class="share-twitter"> \
<iframe class="share-twitter-iframe" \
allowtransparency="true" frameborder="0" scrolling="no" \
src=".html" \
style="width:130px; height:20px;"></iframe> \
</div> \
<div class="share-google"> \
<div class="g-plusone" data-size="small" data-annotation="none"></div> \
</div>',
The only thing we do with this string is populate our social share buttons container when the user mouseovers them:
self.html(obj.shareButtonsHtml);
I know backslash is an escape character in JS. Can anyone explain why my colleague used them here? Surely he didn't need to escape the carriage return or line break? He's since moved on to another pany, so I can't ask him!
One of my colleagues used backslashes when setting the string value of a property in a JS object:
shareButtonsHtml : '\
<span class="share-fb">\
<a href="#" class="fb-share"><span class="fb-share-icon"></span>Share</a>\
<span class="fb-share-count"><i></i><u></u>0</span>\
</span>\
<div class="share-twitter"> \
<iframe class="share-twitter-iframe" \
allowtransparency="true" frameborder="0" scrolling="no" \
src="https://platform.twitter./widgets/tweet_button.html" \
style="width:130px; height:20px;"></iframe> \
</div> \
<div class="share-google"> \
<div class="g-plusone" data-size="small" data-annotation="none"></div> \
</div>',
The only thing we do with this string is populate our social share buttons container when the user mouseovers them:
self.html(obj.shareButtonsHtml);
I know backslash is an escape character in JS. Can anyone explain why my colleague used them here? Surely he didn't need to escape the carriage return or line break? He's since moved on to another pany, so I can't ask him!
Share edited Dec 20, 2024 at 11:02 dumbass 27.3k4 gold badges38 silver badges74 bronze badges asked Jan 31, 2013 at 15:05 And FinallyAnd Finally 5,71414 gold badges75 silver badges112 bronze badges 3-
Looks like he/she was doing
` instead of closing the string
'` and then using+
concatenation to start on a new line. – Mark Pieszak - Trilon.io Commented Jan 31, 2013 at 15:08 - Yes, it does escape the following linebreak. – Bergi Commented Jan 31, 2013 at 15:08
- 1 See How to create multiline strings in javascript – Bergi Commented Jan 31, 2013 at 15:09
3 Answers
Reset to default 5He's escaping newline characters.
That's required if you want multiline strings, otherwise JS wouldn't recognise the next line as part of the string.
So, yes, he did in fact "need to escape the carriage return or line break". That's exactly what he's doing there.
You can use string concatenation to spread strings over multiple lines, but that means extra operations each time you use the string. The performance difference not very significant, it's mostly a matter of preference.
The reason multiline stings don't normally work in JS is because the interpreter (in most cases*) adds a ;
to the end of the line of code, if it isn't there already. This is why the ;
is optional, but it also breaks multiline strings.
* An exception would be object / array literals, ;
's aren't added there, for example:
var obj = {
a:1
}
The backslashes are used to escape the newlines here. It can also be used in the mand line for example.
David Walsh has written an excellent post about multiline javascript strings: http://davidwalsh.name/multiline-javascript-strings
It seems your colleague is actually using the preferred method above the 'slow and ugly' method using the plus (+) signs to concatenate the strings.
From the blog:
Adding a backslash at the end of each line tells the JavaScript engine that the string will continue to the next line, thus avoiding the automatic semicolon insertion annoyance.
This simply allows him to define the string on multiple lines (the \
is escaping the "new line" character).
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744918827a4601003.html
评论列表(0条)