So the basic things should not be recorded, but, restudying ~ first from the data type
JS Six data types: number, String, object, Boolean, null, undefined
String: Single or double quotation marks, such as "string"
Number: What integer AH floating point is called numbers, you know ~
Boolean: It's true and false.
Undefined: Undefined, which is when you create a variable and you don't assign it a value.
Null: So the name is long, NULL is not, nothing to say
Object: It's hard for me to explain that. Except for the five types above.
--------------------above are all clouds, the following is the god horse------------------------------
The data type is judged by typeof
typeof can solve most of the data type judgment, is a unary operation, placed before an operation value, its return value is a string, which describes the type of the operand, so to determine whether a string type, you can directly if (typeof (your value) = = "string") {}
The following are the results of the various data types returned:
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 can determine the type of a function in addition to the data type
This makes it clear that, except for the first four types, null, object, and array returns are of type object;
Functions are returned for function types, such as typeof (Date), typeof (Eval), and so on.
And then here's another big, gray-hot problem, and how do you determine if the data is an array type?
---------------------------------------actually this is my purpose, Baa ~----------------------------------------------
The method of JS to judge array type
method One of instanceof
instance, hence the name of righteousness, instance, example, so instanceof used to judge whether a variable is an instance of an object, is a three-mesh operation---and typeof the most substantial difference
A instanceof B?alert ("true"): Alert ("false")//Note B value is the type of data you want to judge, not a string, such as an array
Give me a chestnut:
method Two of Constructor
Definition in the Consortium definition: constructor property returns a reference to the array function that created this object
is to return the constructor that corresponds to the object. It's not exactly consistent with instanceof, but the effect is the same.
such as: (a instanceof Array)//a is an instance of array? True or False
(A.constructor = = array)//A is the corresponding constructor of an instance an array? True or False
Give me 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 the 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);
-------------------------------------The following is not an original--------------------------------------
A more rigorous and versatile approach:
function IsArray (object) {return
object && typeof object=== ' object ' &&
Array = = Object.constructor;
}
!! Attention:
Using instaceof and Construcor, the array to be judged must be declared on the current page! For example, a page (parent page) has a frame, the frame refers to a page (subpage), in the child page declared an Array, and assign it to the parent page of a variable, then judge the variable, Array = = Object.constructor, return false;
Reason:
1, the array belongs to the reference data, in the transfer process, only refers to the transfer of the address.
2, each page of the array native object refers to the address is not the same, in the child page declared array, the corresponding constructor, is a child page of the array object, the parent page to judge, the use of the array is not equal to the child page array; Remember, it is difficult to track the problem!
method of three characteristic judgment method
There are some flaws in the above methods, but to believe that the wisdom of the masses is omnipotent, we can judge its type according to some characteristics of the array.
function IsArray (object) {return
object && typeof object=== ' object ' &&
typeof object.length== = ' number ' &&
typeof object.splice=== ' function ' &&
//Determine whether the length property is enumerable for the array will get false
! Object.propertyisenumerable (' length '));
Having length and splice is not necessarily an array, because you can add attributes to an object, rather than enumerate the length property, which is the most important judgment factor.
PS: Here to popularize the propertyIsEnumerable method:
Object. propertyIsEnumerable (Proname)
Determines whether the specified property can be enumerated
Note: If proname exists in object and you can use a for ... The propertyIsEnumerable property returns True if the in loop is a poor lift. If object does not have the specified property or the specified property is not enumerable, then the propertyIsEnumerable property returns False.
The propertyIsEnumerable property does not consider objects in the prototype chain.
Example:
var a = new Array ("Apple", "banana", "cactus");
document.write (a.propertyisenumerable (1));
method Four of The easiest way to do it
function IsArray (o) {return
Object.prototype.toString.call (o) = = ' [Object Array] ';
}
The above is the entire contents of this article, learn more JavaScript syntax, you can see: "JavaScript Reference tutorial", "JavaScript Code style guide," And I hope that we support the cloud-Habitat community.