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

While looking through a 3rd party JavaScript API and example code I noticed the following declaration.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744702894a4588878.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信