Apply and call simple usage and judge the pits of arrays

Source: Internet
Author: User


1 typeof and Instanceof

var array = [];
Usually if you determine whether an object is an array, you might use the typeof array, but the output is "object".

typeof generally can only return the following results:
number,boolean,string,function,object,undefined.
For special objects such as Array,null, use typeof to return an object.

So you should:
Array instanceof array output is true;
Of course:
The array instanceof Object output is also true;
Because the array itself is a subset of the object.

This is also why the array of conversion arrays is as follows:
Array.prototype.slice.call (arguments,0);
Of course:
[].slice.call (arguments,0); it's OK.


2 Call and apply


The above refers both to the use of call, so I would like to say a few words about the use of both.

Apply

The Function.apply (Obj,args) method can receive two parameters,
OBJ: This object will replace the This object in the function class
Args: This is an array, which is passed as a parameter to function (Args-->arguments, which refers to the arguments in the function

Instance:
function people (Age,sex) {
This.age=age;
This.sex=sex;
};
function Adult (age,sex) {
People.apply (this,arguments)
};
var jack = new Adult (' man '); Console.log (jack.age+ ":" +jack.sex)
The output is 18:man;
Actually see this piece of code to know how to use it.
In fact, apply in the final analysis is to make an object can use not their own methods, such as: Jack.people (), so certainly not!

See on the web about apply some extension methods:
Maximum value: var array = [n/a]; Math.max.apply (Null,array); 3
Minimum value: var array = [n/a]; Math.min.apply (Null,array); 1
Merge arrays: var array1 = [1,2,3],array2 = [4,5,6]; Array.prototype.push.apply (Array1,array2);
In fact, using the properties of apply to convert an array into a parameter list [param1[,param2[,... [, Paramn]]], to deal with these problems.
The first two methods are handled because the array is not accepted.
The third merge array has many other methods that you can not use.

Call

Function.call (obj,[param1[,param2[,... [, Paramn]]])
In fact the parameter list here [param1[,param2[,... [, Paramn]]] = arguments; Only call must list the parameters;
If you use the above example, the result of the output is the same!

If there are other interesting ways to extend the comments tell me!

Apply and call simple usage and determine the pits of arrays

Related Article

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.