Such basic things should not be recorded anymore, but, reviewing the past and learning the new ones first. Let’s start with the data type.
JS six major data types: number, string, object, Boolean, null, undefined
string: Explained by single or double quotes, such as "string"
number: What integers, floating point numbers are all called numbers, you know ~
Boolean: That's true and false
undefined: undefined, that is, you create a variable but don't assign a value to it~
null: Therefore, it is called Sijiu. null means nothing, nothing means nothing
object: I'm hard to explain this. It's the type other than the above five
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Typeof data type judgment
typeof can solve most data type judgments. It is a one-ary operation. Before an operation value, its return value is a string. This string indicates the type of the operation number. Therefore, if (typeof(your value) == "string"){}
Here are the results returned by various data types:
var a="string"; console.log(a); //stringvar a=1; console.log(a); //numbervar a=false; console.log(a); //booleanvar a; console.log(typeof a); //undfinedvar a = null; console.log(typeof a); //objectvar a = document; console.log(typeof a); //objectvar a = []; console.log(a); //objectvar a = function(){}; console.log(typeof a) //function In addition to judging data types, you can also judge function typesThis is obvious. Except for the first four types, null, object, and array return all object types;
For function types, the function is returned, such as typeof(Date), typeof(eval), etc.
Then here we can extend another problem that is very popular and the solution is already common. How to determine whether the data is an array type?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
JS method to determine array type
Method 1 instanceof
instance, therefore, the name means, instance, example. Therefore, instanceof is used to determine whether a variable is an instance of an object. It is a three-dimensional operation formula--the most substantial difference between typeof
a instanceof b?alert("true"):alert("false") //Note that the b value is the data type you want to judge, not a string, such as Array
Take a chestnut:
var a=[];console.log(a instanceof Array) //Return true
Method 2 constructor
Definition in W3C definition: constructor property returns a reference to the array function that created this object
It returns the constructor corresponding to the object. In definition, it is not very consistent with instanceof, but the effects are the same
For example: (a instanceof Array) //a Is it an instance of Array? true or false
(a.constructor == Array) // Is the constructor corresponding to the instance a corresponding to Array? true or false
Take 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 method to judge various types is:
console.log([].constructor == Array);console.log({}.constructor == Object);console.log("string".constructor == String);console.log((123).constructor == Number);console.log(true.constructor == Boolean);----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
A more rigorous and general method:
function isArray(object){ return object && typeof object==='object' && Array == object.constructor;}! ! Notice:
Using instanceof and constructor, the judged array must be declared on the current page! For example, a page (parent page) has a framework, a page (subpage) is referenced in the framework, an array is declared in the child page, and assigned it to a variable of the parent page. At this time, the variable is judged, Array == object.constructor; will return false;
reason:
1. Array is a reference data. During the transmission process, it is just a transfer of the reference address.
2. The address referenced by the Array native object of each page is different. The corresponding constructor of the array declared in the child page is the Array object of the child page. For judgment on the parent page, the Array used is not equal to the Array of the child page; remember, otherwise it will be difficult to track the problem!
Method 3: Characteristics judging method
All of the above methods have certain flaws, but we must believe that the wisdom of the masses is omnipotent. We can judge its type based on some characteristics of the array.
function isArray(object){ return object && typeof object==='object' && typeof object.length==='number' && typeof object.splice==='function' && //Just determine whether the length property is enumerable for arrays, false will be obtained!(object.propertyIsEnumerable('length'));}Having length and splice is not necessarily an array, because the most important judgment factor is to add attributes to objects, but not enumerating length attributes.
ps: Here we popularize the propertyIsEnumerable method:
object. propertyIsEnumerable(proName)
Determine whether the specified attribute can be listed
Note: If the proName exists in an object and can be exhaustively enumerated using a For…In loop, the propertyIsEnumerable property returns true. If the object does not have the specified property or the specified property is not enumerable, the propertyIsEnumerable property returns false.
The propertyIsEnumerable property does not take into account objects in the prototype chain.
Example:
var a = new Array("apple", "banana", "cactus"); document.write(a.propertyIsEnumerable(1));Method 4 The easiest method
function isArray(o) { return Object.prototype.toString.call(o) === '[object Array]';}The above is the entire content of this article. For more information about JavaScript, you can check out: "JavaScript Reference Tutorial" and "JavaScript Code Style Guide". I also hope that everyone will support Wulin.com more.