最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

syntax - Using '' in javascript to declare string occupying multiple lines - Stack Overflow

programmeradmin2浏览0评论

While looking through a 3rd party JavaScript API and example code I noticed the following declaration. Basically XML assigned to a string but they seem to split it up on multiple lines using '\', I was unaware this could be done in javascript. Could anyone provide some more details on how this works?

Thanks.

var PrimaryChannel = '<ChannelParams ChannelType="Digital"> \
                                <DigitalChannelParams \
                                    PhysicalChannelIDType="Cable" \
                                    PhysicalChannelID="107" \
                                    DemodMode="QAM256" \
                                    ProgramSelectionMode="PATProgram" \
                                    ProgramID="2"> \
                                </DigitalChannelParams> \
                            </ChannelParams>';

While looking through a 3rd party JavaScript API and example code I noticed the following declaration. Basically XML assigned to a string but they seem to split it up on multiple lines using '\', I was unaware this could be done in javascript. Could anyone provide some more details on how this works?

Thanks.

var PrimaryChannel = '<ChannelParams ChannelType="Digital"> \
                                <DigitalChannelParams \
                                    PhysicalChannelIDType="Cable" \
                                    PhysicalChannelID="107" \
                                    DemodMode="QAM256" \
                                    ProgramSelectionMode="PATProgram" \
                                    ProgramID="2"> \
                                </DigitalChannelParams> \
                            </ChannelParams>';
Share Improve this question asked Jul 23, 2010 at 16:36 Ryan SampsonRyan Sampson 6,81712 gold badges50 silver badges55 bronze badges 4
  • That looks funny because the backslashes are inside the quotes... which makes me believe that it isn't really a JS thing and those slashes are either being preserved in the string, or... they're escaping the newline. – mpen Commented Jul 23, 2010 at 16:42
  • @Mark, they ARE escaping the newline – CaffGeek Commented Jul 23, 2010 at 16:43
  • 1 @Chad: Yeah.. but I mean, it's not the same thing as closing the quote, and then escaping the newline, is it? In one case you're just telling JS that the line continues so it shouldn't work is magic-semi-colon BS, and in the other case, you're saying what should be included in the string, no? Or maybe I'm confusing languages and how they handle these things... Python for instance lets you butt two strings up next to each other and it'll just join em for you. – mpen Commented Jul 23, 2010 at 16:51
  • @Chad: Here's what I mean: pastebin./kW7CvjMc – mpen Commented Jul 23, 2010 at 16:56
Add a ment  | 

3 Answers 3

Reset to default 6

It is escaping the newline character, but it's not remended. If you minify your js after the fact, it will break horribly.

You are better off doing something like

var myString = 
   ['line1',
    'line2',
    'line3',
    'line4',
    'line5'].join('\n');

or

var mystring = 
    'line1' + 
    'line2' + 
    'line3' + 
    'line4' + 
    'line5';

most browsers support this. it's not standards-pliant however

Yes, you can do that. \ is the line continuation character in JavaScript.

Edit: It's technically an escape character.

发布评论

评论列表(0)

  1. 暂无评论