JavaScript implements Heredoc syntax and a large number of string concatenation implementations

Source: Internet
Author: User
Tags php and

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

Php:

The code is as follows Copy Code
$sql =<<<eod
SELECT *
From pages
where Pagename= ' $PN '
EOD;

Python:

The code is as follows Copy Code
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:

The code is as follows Copy Code
var str = "
Here's line one
and line two
Finally, Line three!
";

alert (str);

Splicing mode two:

The code is as follows Copy Code
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:

The code is as follows Copy Code
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));


uses func.tostring () to get the string that needs to be processed in batches, using split (/n/). Slice (1,-1) to remove the code defined by the two lines of function, and reassemble it.

refer:http://trentrichardson.com/2010/11/11/javascript-heredoc-like-syntax/

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.