How Javascript determines data and array types
- 2021-06-29 10:04:12
- OfStack
You really shouldn't record anything that's so basic anymore, but start with data types
js6 Big Data Types: number, string, object, Boolean, null, undefined
string: Described by single or double quotation marks, such as "string"
number: Any integer or floating point number is called a number, you know ~
Boolean: true and false
undefined: Undefined, that is, you create a variable and do not assign it a value~
null: So it's a long time ago, null doesn't have any, it doesn't mean anything.
object: This is also a difficult statement for me to explain.Is the type other than the five above
-----------------------------------------------------------------------------------------------
typeof for Data Type Judgment
typeof resolves most data type judgments by placing an unary operation before an operation value and returning a string that indicates the type of operation, so to determine whether an operation is of type String, you can directly if (typeof (your value) == "string"){}
The following are the results returned by various data types:
var a="string"; console.log(a); //string
var a=1; console.log(a); //number
var a=false; console.log(a); //boolean
var a; console.log(typeof a); //undfined
var a = null; console.log(typeof a); //object
var a = document; console.log(typeof a); //object
var a = []; console.log(a); //object
var a = function(){}; console.log(typeof a) //function In addition to the type of data you can determine function type
This makes it obvious that except for the first four types, null, objects, and arrays all return object types.
For function types, function is returned, such as typeof (Date), typeof (eval), and so on.
Then you can deduce another popular and common problem that has been solved. How can you tell if the data is an array type?
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
js Method for Determining Array Type
instanceof for Method 1
instance, so the name means, instance, example, so instanceof is used to determine whether a variable is an instance of an object, and it is a 3-item operation--the most essential difference from typeof
a instanceof b ?alert ("true"): alert ("false")//Note that the b value is the type of data you want to determine, not a string, such as Array
Lift a chestnut:
var a=[];
console.log(a instanceof Array) // Return true
constructor of Method 2
Definition in the W3C definition: The constructor property returns a reference to the array function that created this object
Is the constructor corresponding to the return object.Not exactly the same as instanceof by definition, but the effect is the same
For example: (a instanceof Array) //a Is there an instance of Array?true or false
(a.constructor == Array) // Whether the corresponding constructor for the a instance is Array?true or false
Lift a chestnut:
function employee(name,job,born){
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
console.log(bill.constructor); // output function employee(name, jobtitle, born){this.name = name; this.jobtitle = job; this.born = born;}
Then the way to judge each type is:
console.log([].constructor == Array);
console.log({}.constructor == Object);
console.log("string".constructor == String);
console.log((123).constructor == Number);
console.log(true.constructor == Boolean);
-------------------------------------------------- The following is not an original-----------------------------------
A more rigorous and common approach:
function isArray(object){
return object && typeof object==='object' &&
Array == object.constructor;
}
!!Be careful:
With instaceof and construcor, the judged array must be declared on the current page!For example, a page (parent page) has a frame, a page (sub-page) is referenced in the frame, an array is declared in the sub-page, and assigned to a variable of the parent page, Array == object.constructor;Returns false;
Reason:
1. array is referential data. In the process of transfer, only referencing address is transferred.
2. Array native objects on each page refer to different addresses. array declared on a subpage corresponds to the constructor, which is the Array object on a subpage.The parent page uses an Array that is not equal to the child page's Array.Remember, otherwise it's hard to track the problem!
Method 3 Property Judgment
All of the above methods have some drawbacks, but to believe that the wisdom of the people is omnipotent, we can judge their type by some of the properties of the array.
function isArray(object){
return object && typeof object==='object' &&
typeof object.length==='number' &&
typeof object.splice==='function' &&
// judge length Is the property enumerable For arrays Will get false
!(object.propertyIsEnumerable('length'));
}
The presence of length and splice is not necessarily an array, because it is possible to add attributes to an object instead of enumerating the length attributes that are the most important determinants.
ps: Popularize the propertyIsEnumerable method here:
object. propertyIsEnumerable(proName)
Determines whether a specified property is enumerable
Note: If proName exists in object and can be enumerated using an For... In loop, the propertyIsEnumerable attribute returns true.If object does not have the specified property or the specified property is not enumerable, the propertyIsEnumerable property returns false.
The propertyIsEnumerable attribute does not consider objects in the prototype chain.
Example:
var a = new Array("apple", "banana", "cactus");
document.write(a.propertyIsEnumerable(1));
The easiest way to do method 4
function isArray(o) {
return Object.prototype.toString.call(o) === ' [object Array] ' ;
}