The following is a summary of the six methods for distinguishing objects from each other in JavaScript. I think this is quite good. Now I will share it with you and give you a reference. Let's take a look at the following issues:
When determining the type of an object in JS, typeof is usually used. The problem arises because typeof () returns an object when identifying an array, so JS
To determine whether an object is an array requires some special processing methods. The following describes the six processing methods summarized by the individual.
2. Open Doors
To determine whether an object is an array during development, we recommend using the following function:
function isArray(obj){ if(Array.isArray){ return Array.isArray(obj); }else{ return Object.prototype.toString.call(obj)==="[object Array]"; }}
The above function is convenient for anyone who is eager to solve the problem. I will describe six methods in detail below, because the examiner may need a comprehensive understanding of you during the interview;
Three or six solutions:
(1) Method 1: Use the toString Method
Call toString () to convert the variable to a string of its type. This method is feasible for the real array; when the parameter object is converted to a string
If [object Arguments] is returned, the conversion fails. In addition, the conversion fails for the object class containing the numeric Length attribute.
The method is as follows:
Array Judgment Method
Script function isArrayOne (arr) {return Object. prototype. toString. call (arr) = "[object Array]";} var obj = {"k1": "v1"}; var arr = [1, 2]; console. log ("Object Result:" + isArrayOne (obj); console. log ("array Result:" + isArrayOne (arr); script
Result
Note: We recommend that you use "=" instead of "=" because it is more efficient!
(2) Method 2: Using isArray:
Using Javascript 1.8.5 (ECMAScript 5), the variable name. isArray () can achieve this purpose, provided that this function is supported. In fact, isArray is
Method 1 is encapsulated and used.
The usage is very simple:
Array. isArray (obj); // obj is the object to be detected.
Returns true or false. If it is true, it is an array.
(3) method 3: Use the instanceof operator to determine,
Note: The left side of the instanceof operator is the sub-object (object to be tested), and the right side is the parent Constructor (Array here ),
That is, the sub-object instanceof parent Constructor
Instance: all objects created using the new Constructor () are called constructor instances.
I am confused for a long time, but I still can check the Code directly:
DocumentScript var obj = {"k1": "v1"}; var arr = [1, 2]; console. log ("Instanceof processing object Result:" + (obj instanceof Array); console. log ("Instanceof processing Array results:" + (arr instanceof Array); script
The running result is as follows:
Document 《script》var obj = {'k':'v'};var t1 = new Array(1);var t2 = t1;console.log(obj.constructor == Array);console.log(t1.constructor == Array);console.log(t2.constructor == Array); 《script》
Result
(6) Use typeof (object) + type name to judge:
The Code is as follows:
Document 《script》function isArrayFour(arr){if(typeof(arr)==="object"){if(arr.concat){return "This is Array";}else{return "This Not Array";}}}var arr = [1];var obj = {'k':'v'};console.log(typeof(arr));console.log(typeof(obj));console.log(isArrayFour(arr));console.log(isArrayFour(obj)); 《script》
The result is as follows:
In fact, this method has limitations. Some of you may have cracked it.
What should I do if this attribute is accidentally defined in the object?
var obj = {'concat':'Teast me?'};
The above six methods of distinguishing objects from each other in JS are all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support PHP.
For more articles about JS methods for distinguishing objects from each other, see PHP!