How to stitch a large number of strings in JavaScript _javascript tips

Source: Internet
Author: User
Tags function definition php and

There are heredoc ways to define strings in PHP and Python:

Php:

Copy Code code as follows:

$sql =<<<eod
SELECT *
From pages
where Pagename= ' $PN '
EOD;

Python:
Copy Code code as follows:

Print "" "
This is a example of a string in the Heredoc syntax.
This text can span multiple lines
"""

JS concatenation of a large number of strings no heredoc style operators are more cumbersome:

Splicing Way One:

Copy Code code as follows:

var str = "\
Here's line one \
and line two \
Finally, Line three! \
";

alert (str);


Splicing mode two:
Copy Code code as follows:

var __template =
' <tr> ' +
' <td> #salarySN #</td> ' +
' <td> #name #</td> ' +
' <td> #TDR_NAME #</td> ' +
' <td> #TSD_NAME #</td> ' +
' <td> #WORK_STATUS #</td> ' +
' <td> #isleader_display #</td> ' +
' <td> '
+ ' <a href= ' javascript:void ( -1) ' > Set role </a> '
+ ' </td></tr> ';

JS strings need to break the original string style, each row processing, this is a bit unbearable.

Give a solution:

Copy Code code as follows:

function Aheredoc () {/*
Hello, world!.
I am a JavaScript here document.
Use the ' HereDoc ' function to extract me.
*/}

function HereDoc (func) {
Return func.tostring (). Split (/\n/). Slice (1,-1). Join (' \ n ');
}
Console.log (HereDoc (Aheredoc));


Use Func.tostring () to get the strings that need to be processed in batches, using split (/\n/). Slice (1,-1) to remove the code from the two lines of function definition and reassemble it.

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.