Javascript concatenates a large number of strings.
In php and python, there is a heredoc string definition method:
Php:
Copy codeThe Code is as follows:
$ SQL = <EOD
Select *
From pages
Where pagename = '$ pn'
EOD;
Python:
Copy codeThe Code is as follows:
Print """
This is an example of a string in the heredoc syntax.
This text can span multiple lines
"""
Js concatenates a large number of strings without heredoc-style operators, which is complicated:
Splicing Method 1:
Copy codeThe Code is as follows:
Var str = "\
Here is line one \
And line two \
Finally, line three! \
";
Alert (str );
Splicing Method 2:
Copy codeThe Code is 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 a role </a>'
+ '</Td> </tr> ';
JS Strings need to break the original string style, and each line is processed, which is a bit unbearable.
Provide a solution:
Copy codeThe Code is 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 obtain the string to be processed in batches. Use split (/\ n /). slice (1,-1) removes the first and last line of function-defined code and reassembles it.