Create a StringBuffer class based on array in JavaScript
Description
The following code shows how to create a StringBuffer class based on array.
Example
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function StringBuffer() {<!-- w w w . j ava2 s. c o m-->
this.__strings__ = new Array;
}
StringBuffer.prototype.append = function (str) {
this.__strings__.push(str);
};
StringBuffer.prototype.toString = function () {
return this.__strings__.join("");
};
var buffer = new StringBuffer();
for (var i=0; i < 100; i++) {
buffer.append("text");
}
var result = buffer.toString();
document.write(result.length());
</script>
</head>
<body>
</body>
</html>
The code above generates the following result.
Javascript Tutorial String Format
Create a big element from string object in ...
Create a font element from string object an...
Create a font element from string object an...
Create a small element from string object i...
Create a strike element from string object ...
Create a string from character code in Java...
Create a string object and then calls it su...
Create a string type variable with either d...
Create a string with the String constructor...
Create a sub element from string object in ...
Create a sup element from string object in ...
Create a tt element from string object in J...
Create an anchor link element from string o...
Create an anchor link from string object in...
Create an i element from string object in J...
Create a StringBuffer class based on array ...
Create a b element from string object in Ja...Create a big element from string object in ...
Create a font element from string object an...
Create a font element from string object an...
Create a small element from string object i...
Create a strike element from string object ...
Create a string from character code in Java...
Create a string object and then calls it su...
Create a string type variable with either d...
Create a string with the String constructor...
Create a sub element from string object in ...
Create a sup element from string object in ...
Create a tt element from string object in J...
Create an anchor link element from string o...
Create an anchor link from string object in...
Create an i element from string object in J...