Array,prototype.concat.apply and [].conat.apply.

Source: Internet
Author: User

Always know that JS array built-in object has a concat method, but did not do much research, today by chance to look at

concat is connecting one or more arrays

Returns a copy of the array after the connection

var oldarr=[];

var arr=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]];

var newarr=oldarr.conat (arr);

Console.log (NEWARR);

Console.log (Oldarr); //[] did not change

So I thought I would connect each item of the array to the Oldarr as an array.

This requires the Apply method to change the array to parameter list arguments

Console.log (Array.prototype.concat.apply (Array,arr));

It turned out strange.

No results were thought of.

What the hell is the arrow place???

Think about it.

Array.prototype.concat.apply (Array,arr);

Not that the array called the concat on Array.prototype and then passed in the parameter arr

Changing the array to number String is not the way to borrow the Concat method on Array.prototype.

Then add the caller to the 0-bit position of the array

I feel that it is arr.unshift (Array);

Anything else, it's going to push.

Let's see the picture below.

Console.log (Array.prototype.concat.apply (Number,arr));

Console.log (Array.prototype.concat.apply (String,arr));

Console.log (Array.prototype.concat.apply (Regexp,arr));

that's what it looks like.

So change the parameters inside the aplly to []

Console.log (Array.prototype.concat.apply ([],arr));

You can simply write [].concat.apply ([],arr);

Arrays can not only concatenate array arrays but also concatenate string numeric objects, etc.

Console.log ([].concat ({a:2,b:3,c:3}));

He's going to put the object in the array, and there's no connection. I have you .

Console.log ([].concat ("123"}));

According to these I have summed up if the concat passed in the parameter is not an array will not traverse direct violence added to the array item

When the argument is an array, it traverses the

Need to pay attention to is not silly to use String.Concat (); Number.concat (), etc.

Above is to use the Apply method to change this point to the borrowed

It's not concat itself.

String.prototype there are concat but the results are not the same drop!

It's just my personal understanding that I want to help you.

Array,prototype.concat.apply and [].conat.apply.

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.