Example of concatenation of strings using array join in JS

Source: Internet
Author: User
Tags stringbuffer


1 (function (window) {
var core_arrpro = Array.prototype;
3 var core_slice = Core_arrpro.slice;
4 var core_push = Core_arrpro.push;
5 var core_unshift = Core_arrpro.unshift;
6
7 function StringBuffer () {
8 this.buffer = [];
9}
Ten Stringbuffer.prototype = {
One push:function () {
Core_push.apply (This.buffer, Core_slice.call (arguments));
return this;
14},
Unshift:function () {
Core_unshift.apply (This.buffer, Core_slice.call (arguments));
return to this;
18},
Tostring:function () {
return This.buffer.join (');
21}
22};
return window. StringBuffer = StringBuffer;
) (window);
document.getElementById (' result '). InnerHTML = new StringBuffer (). push (' ASDASD '). Unshift (' 654 ', 123). Push (123, 564 , ' SDF ');


Because the apply efficiency is lower than call, and most push operations have only 1 parameters, the update is as follows:

(function (window) {
var core_arrpro = Array.prototype;
var core_slice = Core_arrpro.slice;

function StringBuffer () {
This.buffer = [];
}
Stringbuffer.prototype = {
Push:function () {
if (arguments.length = = 1) {
This.buffer.push (Arguments[0])
}else if (Arguments.length > 1) {
This.buffer = This.buffer.concat (core_slice.call (arguments));
}
return this;
},
Unshift:function () {
if (arguments.length = = 1) {
This.buffer.unshift (Arguments[0])
}else if (Arguments.length > 1) {
This.buffer = core_slice.call (arguments). Concat (This.buffer)
}
return this;
},
Tostring:function () {
Return This.buffer.join (");
}
};
return window. StringBuffer = StringBuffer;
}) (window);
            document.getElementById (' result '). InnerHTML = New StringBuffer (). push (' ASDASD '). Unshift (' 654 ', 123). Push (123, 564, ' SDF ');

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.