When we write the front-end JS, many strings are often spliced through "+" and then mounted to a DOM element. However, I will not compare the effect of using "+" to splice strings in different browsers. There are many such comparisons on the Internet. Many cool people say that using the join method of array in JS to concatenate strings is very good. To this end, write a JS class in the project to uniformly process String concatenation.
Code
// A custom string connection class for concatenating strings, which improves performance compared to "+ ".
Function stringbuffer (){
This . _ STRs = New Array ();
}
Stringbuffer. Prototype. append = Function (STR ){
This . _ STRs. Push (STR );
};
Stringbuffer. Prototype. arraytostring = Function (){
Return This . _ STRs. Join ( "" );
};
When using this class, we can directly use the following method:
VaR strbuff=NewStringbuffer ();
Strbuff. append ("Hello,");
Strbuff. append ("Welcome to JavaScript!");
Alert (strbuff. arraytostring ());