- JavaScript Unlocked
- Dmitry Sheiko
- 551字
- 2021-07-30 09:57:00
Mastering multiline strings in JavaScript
Multi-line strings aren't a good part of JavaScript. While they are easy to declare in other languages (for instance, NOWDOC), you cannot just keep single-quoted or double-quoted strings in multiple lines. This will lead to syntax error as every line in JavaScript is considered as a possible command. You can set backslashes to show your intention:
var str = "Lorem ipsum dolor sit amet, \n\ consectetur adipiscing elit. Nunc ornare, \n\ diam ultricies vehicula aliquam, mauris \n\ ipsum dapibus dolor, quis fringilla leo ligula non neque";
This kind of works. However, as soon as you miss a trailing space, you get a syntax error, which is not easy to spot. While most script agents support this syntax, it's, however, not a part of the EcmaScript specification.
In the times of EcmaScript for XML (E4X), we could assign a pure XML to a string, which opened a way for declarations such as these:
var str = <>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ornare </>.toString();
Nowadays E4X is deprecated, it's not supported anymore.
Concatenation versus array join
We can also use string concatenation. It may feel clumsy, but it's safe:
var str = "Lorem ipsum dolor sit amet, \n" + "consectetur adipiscing elit. Nunc ornare,\n" + "diam ultricies vehicula aliquam, mauris \n" + "ipsum dapibus dolor, quis fringilla leo ligula non neque";
You may be surprised, but concatenation is slower than array joining. So the following technique will work faster:
var str = [ "Lorem ipsum dolor sit amet, \n", "consectetur adipiscing elit. Nunc ornare,\n", "diam ultricies vehicula aliquam, mauris \n", "ipsum dapibus dolor, quis fringilla leo ligula non neque"].join( "" );
Template literal
What about ES6? The latest EcmaScript specification introduces a new sort of string literal, template literal:
var str = `Lorem ipsum dolor sit amet, \n consectetur adipiscing elit. Nunc ornare, \n diam ultricies vehicula aliquam, mauris \n ipsum dapibus dolor, quis fringilla leo ligula non neque`;
Now the syntax looks elegant. But there is more. Template literals really remind us of NOWDOC. You can refer any variable declared in the scope within the string:
"use strict"; var title = "Some title", text = "Some text", str = `<div class="message"> <h2>${title}</h2> <article>${text}</article> </div>`; console.log( str );
The output is as follows:
<div class="message"> <h2>Some title</h2> <article>Some text</article> </div>
If you wonder when can you safely use this syntax, I have a good news for you—this feature is already supported by (almost) all the major script agents (http://kangax.github.io/compat-table/es6/).
Multi-line strings via transpilers
With the advance of ReactJS, Facebook's EcmaScript language extension named JSX ( influenced by previously mentioned E4X, they proposed a kind of string literal for XML-like content without any screening at all. This type supports template interpolation similar to ES6 templates:
"use strict"; var Hello = React.createClass({ render: function() { return <div class="message"> <h2>{this.props.title}</h2> <article>{this.props.text}</article> </div>; } }); React.render(<Hello title="Some title" text="Some text" />, node);
Another way to declare multiline strings is by using CommonJS Compiler (compiler transforms any content that is not .js
/.json
content into a single-line string:
foo.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ornare, diam ultricies vehicula aliquam, mauris ipsum dapibus dolor, quis fringilla leo ligula non neque
consumer.js
var str = require( "./foo.txt" ); console.log( str );
You can find an example of JSX use in Chapter 6, A Large-Scale JavaScript Application Architecture.
- Android Jetpack開發:原理解析與應用實戰
- Boost C++ Application Development Cookbook(Second Edition)
- Oracle從新手到高手
- 構建移動網站與APP:HTML 5移動開發入門與實戰(跨平臺移動開發叢書)
- Mastering Natural Language Processing with Python
- 華為HMS生態與應用開發實戰
- 數據結構(Python語言描述)(第2版)
- VMware vSphere 6.7虛擬化架構實戰指南
- Java Web程序設計
- Mastering Julia
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- Symfony2 Essentials
- Swift語言實戰精講
- Learning Laravel's Eloquent
- Odoo 10 Implementation Cookbook