Get latest version
Based on the improvement on the 12th floor, the connection is supported:
1
/*
2
* @ Author: uedsky
3
* @ Version: 1.1
4
*/
5
6
/**
7
* @ Class string Concat
8
* @ Return {strbuf/string}
9
* @ Constructor
10
* Eg:
11
VaR Buf = new strbuf ("contructor STR \ n ");
12
Buf. Push ("hello ,")
13
. Push ("Today is {0}, {1}", "Monday", "March 28th ")
14
. Push ("$ {name} is a good $ {category} company", {name: "google", category: "intenet "});
15
Document. Write (BUF); // auto call tostring Method
16
Console. Log (BUF );
17
Console. Log (strbuf ("static {0} method", "INVOKE "));
18
*/
19VaR strbuf = function (s ){
20This. Data = [];
21
If(S ){
22VaR ARGs = arguments, Buf;
23
If(This
InstanceofStrbuf ){
24This. Push. Apply (this, argS );
25}
Else{
// Static invoke
26Buf =
NewStrbuf ();
27
ReturnBuf. Push. Apply (BUF, argS). tostring ();
28}
29}
30};
31Strbuf. Prototype = {
32
// Add string to the instance
33Push: function (s, j ){
34VaR ARGs = arguments;
35
If(ARGs. Length <2 ){
36This. Data. Push (S | "");
37}
Else
If(
TypeofJ = 'object '){
38This. Data. Push (S. Replace (/\ $ \ {([\ W.] +) \}/g, function ($, $1 ){
39
Return($1
InJ )? J [$1]: $;
40}));
41}
Else{
42This. Data. Push (S. Replace (/\ {(\ D +) \}/g, function ($, $1 ){
43
ReturnARGs [+ $1 + 1];
44}));
45}
46
ReturnThis;
47},
48Tostring: function (){
49
ReturnThis. Data. Join ("");
50}
51};
Recently, when I was reading the code written by a colleague, there was a string with a bunch of "+" numbers. After reading the code for half a day, what content will be output tomorrow,
Just think of a class connected with a string, and write the previous method into the class method to facilitate the call,
Supports instance call and static call
The parameter can be a separate string, JSON format, or an array similar to the parameter, as shown in the following example.
/*
* @ Author: uedsky
* @ Version: 1.0
*/
/**
* @ Class string Concat
* @ Return {strbuf/string}
* @ Constructor
*/
VaR strbuf = function (s ){
This. Data = [];
If (s ){
VaR ARGs = arguments, Buf;
If (this instanceof strbuf ){
This. Push. Apply (this, argS );
} Else {// static invoke
Buf = new strbuf ();
Buf. Push. Apply (BUF, argS );
Return Buf. tostring ();
}
}
};
Strbuf. Prototype = {
// Add string to the instance
Push: function (s, j ){
VaR ARGs = arguments;
If (ARGs. Length <2 ){
This. Data. Push (S | "");
} Else if (typeof J = 'object '){
This. Data. Push (S. Replace (/\\ \{ ([\ W.] +)}/g, function ($, $1 ){
Return ($1 in J )? J [$1]: $;
}));
} Else {
This. Data. Push (S. Replace (/\ {(\ D +)}/g, function ($, $1 ){
Return ARGs [+ $1 + 1];
}));
}
},
Tostring: function (){
Return this. Data. Join ("");
}
}; Call example:
VaR Buf = new strbuf ("contructor STR \ n ");
Buf. Push ("hello ,");
Buf. Push ("Today is {0}, {1}", "Monday", "March 28th ");
Buf. Push ("$ {name} is a good $ {category} company", {name: "google", category: "intenet "});
Document. Write (BUF); // auto call tostring Method
Console. Log (BUF );
Console. Log (strbuf ("static {0} method", "INVOKE "));