Multi Line Strings: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (separate sentences on separate lines make smaller diffs. slight grammatical tweaks.)
Line 57: Line 57:
It can be used to pull out a (potentially long, multi-line) string from a dummy XML document.
It can be used to pull out a (potentially long, multi-line) string from a dummy XML document.


<pre class='sample'>
<pre class='sample-bad'>
var longString = <str><![CDATA[
var longString = <str><![CDATA[
   Lorem ipsum dolor sit amet,
   Lorem ipsum dolor sit amet,
Line 73: Line 73:
* Not too efficient; involves the creation and throwaway of an XML DOM.
* Not too efficient; involves the creation and throwaway of an XML DOM.
* Not available in all browsers. For example, Google Chrome does not support E4X.
* Not available in all browsers. For example, Google Chrome does not support E4X.
Cons:
* E4X [https://developer.mozilla.org/en-US/docs/E4X is deprecated].  It will stop working in phases, Firefox 16 through 18.


[[Category:Coding Tips:General]]
[[Category:Coding Tips:General]]

Revision as of 19:11, 28 August 2012

Sometimes, it is desirable to use a multi line string in JavaScript, such as when adding css styles. Here are some approaches, and discussion about the strengths and weaknesses of each approach.

Concatenation

The basic method:

var longString = "Lorem ipsum dolor sit amet, " +
  "venenatis penatibus etiam. " +
  "Nec purus cras elit nec. " +
  "Elit pharetra hymenaeos. " +
  "Donec at cubilia pulvinar elit. " +
  "Aliquet pretium tortor montes maecenas ante amet vel bibendum.";

Pros:

  • Simple to understand.

Cons:

  • Extra syntax at the head and tail of every line.
  • JavaScript string concatenation has poor performance characteristics.

A very similar but more efficient approach would define an array of many strings, then join them into one long string:

var longString = ["Lorem ipsum dolor sit amet, ",
  "venenatis penatibus etiam. ",
  "Nec purus cras elit nec. ",
  "Elit pharetra hymenaeos. ",
  "Donec at cubilia pulvinar elit. ",
  "Aliquet pretium tortor montes maecenas ante amet vel bibendum."
  ].join("");

Line continuation

JavaScript can continue lines, via trailing backslashes, like C:

var longString = "Lorem ipsum dolor sit amet, \
  venenatis penatibus etiam. \
  Nec purus cras elit nec. \
  Elit pharetra hymenaeos. \
  Donec at cubilia pulvinar elit. \
  Aliquet pretium tortor montes maecenas ante amet vel bibendum.";

Pros:

  • Efficient.

Cons:

  • An uncommon technique, therefore not as well understood.
  • Requires extra syntax, albeit not as much as with concatenation.

E4X

The E4X technology allows embedding XML documents directly in JavaScript. It can be used to pull out a (potentially long, multi-line) string from a dummy XML document.

var longString = <str><![CDATA[
  Lorem ipsum dolor sit amet,
  venenatis penatibus etiam.
  Nec purus cras elit nec.
  Elit pharetra hymenaeos.
  Donec at cubilia pulvinar elit.
  Aliquet pretium tortor montes maecenas ante amet vel bibendum.
]]></str>.toString();

Pros:

  • Does not require escape characters for quotes or for new lines; E4X is interpreted verbatim.

Cons:

  • Not too efficient; involves the creation and throwaway of an XML DOM.
  • Not available in all browsers. For example, Google Chrome does not support E4X.

Cons:

  • E4X is deprecated. It will stop working in phases, Firefox 16 through 18.