JavaScript: Multi-line string

Source: Internet
Author: User

Original: http://www.cnblogs.com/ziyunfei/archive/2012/10/04/2711551.html

The function of a multiline string is to improve the readability of the source code. Especially when you are working with a predefined long string, dividing the string into multiple lines can help improve the readability and maintainability of your code. In some languages, a multiline string is also used to make code comments.
Most dynamic scripting languages support multiple lines of string, such as Python, Ruby, and PHP. But what about JavaScript?

Note: Python can use two kinds of multi-line string notation, one is the use of three quotation marks, is also a Python multi-line Comment method, this method produces a multi-line string is a true multi-line. That is, the interpreter will assume that there is a "\ n" at the end of each line:

>>>print ("" "I You He" "") I you He

Another way of writing is

>>>print ("I you He") I you He

This is also the following JavaScript is the implementation of the current writing, it produces only a single line of string. But calling it multi-line refers to the fact that the string occupies more than one physical line in the source code (the corresponding concept is a logical line).

Ruby, in PHP, implements multiple lines of string through here document.

Have you seen the following conclusions on the Internet?

"JavaScript does not support multi-line strings."

"Multiline string is not a legitimate JavaScript syntax."

"This feature is a private implementation that has not been standardized."

If so, forget about them, that's not true. JavaScript actually supports multiple lines of string.

One of the strangest things about JavaScript is that although the language has a very good normative document ECMA-262. There are many misconceptions about the language. There are too many articles on the web that describe inappropriate or complete errors.

Of course, everyone has the right to choose what they want to read, and to choose whether or not to believe the conclusions given in the article. But when I read about JavaScript on my blog, on the mailing list, on Twitter, or anywhere else, I always do the testing, The main thing is that I will open the ECMA-262 and compare the information given in the text with the document to see if it is what he says. Maybe you'll ask me, "Why do you read other people's articles?". Yes, since we have the ECMA-262 documentation, why do we have to read other people's articles? First, the specification document is more difficult to read and difficult to understand. You have to be familiar with the abstract things that are mentioned in the documentation to really understand it. And you have to read the whole story. If you skip some words or sentences, You'll probably understand the mistake.

Articles written in blogs can be viewed as higher-level documents. It usually means that the author has read the document and explained the contents of the document in more understandable words and snippets. Such articles are considered good articles.

A multiline string is an example of what I said above. It is indeed part of the formal specification (ECMA 265 5th edition), ES5 extends the syntax of string literals. Added in (7.8.4 string literals) DoubleStringCharacter and SingleStringCharacter : LineContinuation .

LineContinuationThe syntax is:

\ lineterminatorsequence

LineTerminatorSequenceRefers to one of the following characters:

Line break <LF>
Carriage return character <CR>
Row separator <LS>
Segment Separator <PS>

In other words, you can implement a multiline string by escaping the line terminator. You have to do this, or you will get a syntax error.

var htmlstr = ' \
<div>\
Content\
</div>\
‘;

When he is, this writing only supports the ECMA-262-5 environment. (The Translator notes: In fact, ES3 's environment is all supported)
However, it is important to note that when you output a string, the newline character is not displayed. If you need a real line break, you must precede the backslash with one \n .

If you need to be compatible with the old engine environment, you should use multiple strings to connect, or put multiple strings in an array of elements, and finally join them

var htmlstr = ' <div> ' +
' Content ' +
' </div> ';
var htmlstr = [
' <div> ',
' Content ',
' </div> '
].join (");

JavaScript-style multiline strings are a bit of a clown and are prone to error. But the good news is that in future versions of the specification there will be one Template Strings : http://wiki.ecmascript.org/doku.php?id=harmony:quasis.

JavaScript: Multi-line string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.