Array prototype method call and function apply call when class array parameter under IE8 problem

Source: Internet
Author: User

When a function is called by apply, the method of argument is an array or array of classes (an object with a length property) consisting of various parameters, and the number of arguments passed depends on the value of length, for example, an object args.length=3; Apply will pass Args[0],args[1],args[2] three parameters, if the corresponding value does not exist, then passed into the undefined.

For example:

function F (a,b,c) {console.log (a,b,c);} F.apply (null,{0:123,1:456,2:789,length:2});  123 456 Undefined, because the value of the length property is 2, the value is only passed in 2 parameters f.apply (null,{10:123,11:456,12:789,length:3});  undefined undefined undefined because the parameter object does not have a value of 0, 1, 2 as key       

However, there are several issues to be aware of in IE8 and browsers below IE8.

1. The Apply parameter does not accept objects like {0: ' A ', 1: ' B ', length:2}, which can be a collection of nodes, such as arrays, arguments, Htmlcollection objects, and Nodelist objects.

In this case you may want to convert the parameter to an array.

2. The node collection cannot invoke the array's prototype method, but it is similar to {0: ' A ', 1: ' B ', length:2} objects.

 var nodes = document.getElementsByTagName ("div" var obj = {0:1,1:2,2:3,length:3}; var args = Array.prototype.slice.apply (nodes,[0]); //error, "Array.prototype.slice: ' This ' is not a JavaScript object", The node collection cannot call the array prototype method var args = Array.prototype.slice.apply (obj,[0]); //okvar args = Array.prototype.concat.apply ([],nodes); //okvar args = Array.prototype.concat.apply ([],obj); //error, "Function.prototype.apply: Missing Array or Arguments object", Normal object cannot be used for apply parameter              

Integrated above, you can use Try-catch

var nodes = document.getElementsByTagName ("div"); var obj = {0:1,1:2,2:3,length:3};var params = nodes;//try nodes and OBJTR y{    var args =  Array.prototype.slice.apply (params, [0]);} catch (Err) {    var args = Array.prototype.concat.apply ([],params);} Console.log (args);

In addition, there may be such a:

var args = array.apply ([],params); //var obj = {0:3,length:1};  When var args = Array.apply ([]// in obj[0] conforms to the range of array length, an empty array of length 3 is obtained instead of [3], which is more prone to neglect args.length;  3 (Obj[0]<math.pow (2,32)-1)         

About Concat:

The concat method of an array can be called by any object to get an array, but if the caller is not an array, then the caller itself is only the first element of the result array

functionf(a,b){  varargs = [].concat.apply(arguments,[111,222,333]);  console.log(args);  console.log(args.length);  console.log(args[0]);}f("x","y") //执行结果[Arguments[2], 111, 222, 333]4["x", "y"]

Array prototype method call and function apply call when class array parameter under IE8 problem

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.