The implementation method of embedding a large number of string constants in JavaScript _javascript tips

Source: Internet
Author: User

Small number of good, many words on a dense pile of text, exquisite artistic youth, will use a large number of character join symbols even with indentation, forced into several lines. For example:

Copy Code code as follows:

var html =
' <div> ' +
' <p>Hello</p> ' +
' <p>world ' </p> ' +
' </div> ';

That's fine, if there's a lot of double quotes in the string, it's even more troublesome, and the various escape characters look dazzling.

In fact, there is a small little skill, it can solve the problem. People always think that strings must be in "..." or ' ... ', that's right. But there is also a place where the string can also be preserved as code semantics, which is the ToString of a function, which outputs the entire code of the functions as a string-the annotation part of which is reserved!

So we write an empty function with a/**/annotation, which is the constant content we want. After the ToString plus a regular will be able to extract what we want!

Try it now:

Copy Code code as follows:

var res_code = _text (function () {/*
#include <iostream>

int main ()
{
Std::cout << "Hello World" << Std::endl;
return 0;
}
*/});

var res_poem = _text (function () {/*
More blowing, stars like rain.
BMW Carving Car Incense full road.
Feng Xiao Sound move, the jade pot light turn, one night fish dragon dance.
The Moth is the golden wisp of snow Willow,
The laughter of the incense went.
The crowd searched him for 1100 degrees,
Suddenly looking back, the man is in the dim lights.
*/});

var res_xml = _text (function () {/*
<projectDescription>
<name>Hello</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.adobe.flexbuilder.project.flexbuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.adobe.flexbuilder.project.apollobuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
</projectDescription>
*/});

function _text (Wrap) {
Return wrap.tostring (). Match (/\/\*\s ([\s\s]*) \s\*\//) [1];
}
alert (Res_code);
alert (RES_POEM);
alert (res_xml);

Ok! is the chrome in the comments in front of each line tab removed, if just put the code is not a big problem ~ Of course, there is a prerequisite is the word character can not appear * *

It is important to note that when compressing the code, the annotation is filtered, and part of the manual exclusion is required.

Related Article

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.