with the increase of programming practice, it is very important to find out the detection of data type slowly. I think the procedure is to process the data and display the data. Therefore, the detection of data is also important for programming. Because only the input that meets our expectations is likely to produce the correct output. As we all know, JavaScript is a weakly typed language, which brings a lot of convenience, but also brings a lot of problems. In order to reduce the problem of variable judgment in programming practice, we need to detect uncertain variables to ensure the processing of data that is expected. This article is a summary of the data detection.
1. Detecting Raw values
ECMAScript has 5 primitive types (primitive type), namely Undefined, Null, Boolean, number, and String. For raw values, we can use typeof to detect.
1 //Detecting Strings2 typeof"Str"//returns "string"3 4 //Detecting Numbers5 typeof123//returns "Number"6 7 //Detecting Boolean values8 typeof true //returns "Boolean"9 Ten //detection undefined One typeofUndefined//back to "undefined" A - //detect null - typeof NULL //returns "Object"
The typeof operator has a unique place where we don't get an error when we detect an undeclared variable. undefined variables and undefined variables will return "undefined" through typeof detection.
Also, when we use typeof to detect null variables, we return "object" instead of "null". null is generally not used for instrumentation statements, and simple and null comparisons typically do not contain enough information to determine whether the type of the value is legitimate. However, when the expected value is determined to be null, it can be compared directly with null (= = = and!==).
2. Detecting reference values
Reference values, also known as objects, are references to values other than the original values in JS, such as Object, Function, Array, String, Boolean, number, date, and so on. Since "Object" is returned when using typeof to detect reference types, we do not really get the details of the reference type. The instanceof operator solves this problem, and the instanceof operator is similar to the typeof operator to identify the type of object being processed. Unlike the TypeOf method, the Instanceof method requires the developer to explicitly confirm that the object is of a particular type. The basic usage is:
1 instanceof Constructor
The meaning is to determine whether a value is an instance of the constructor object, and if so, returns True, otherwise false.
Instanceof not only stops at detecting the constructor that constructs the object, it also detects it along the prototype chain. Examples are as follows:
1 var New Date (); 2 3 instanceof Date); // Output: True 4 instanceof // Output: True
By default, each object inherits from object, so the value instanceof object of each object returns True, and using this method to determine whether an object belongs to a particular type is not the best practice.
The instanceof operator can also be used to detect custom types, as shown in the following example:
1 functionBasictype () {2 This. property=true;3 4 This. Getbasicvalue =function(){5 return This. property;6 };7 }8 9 functionNewType () {Ten This. subproperty=false; One } A -Newtype.prototype =NewBasictype (); - varTest =NewNewType (); the -Console.log (testinstanceofNewType);//Output: True -Console.log (testinstanceofBasictype);//Output: True -Console.log (testinstanceofObject);//Output: True
The best way to detect custom types in JavaScript is to use the instanceof operator, which is also the only method.
3. Detection Properties
Another scenario in which null (and undefined) is used is when detecting whether an attribute exists in an object, such as:
1 //bad wording: detect false values2 if(Object[propertyname]) {3 //Code4 }5 6 //bad notation: comparison with null7 if(Object[propertyname]! =NULL) {8 //Code9 }Ten One //bad writing: compare with undefined A if(Object[propertyname]! =undefined) { - //Code -}
In the example above, the value of the property is actually detected by the given property name, not the existence of the property, because when the property value is false, the result exceeds our expectations, such as 0, "", false, NULL, and undefined. If we want to detect a property value of null or undefined, which of the above is wrong.
The best way to determine whether a property exists is to use the in operator. The In operator only determines whether a property exists or not, regardless of the property value, which avoids the problem above.
1 varObject = {2count:0,3NameNULL4 };5 6 //good wording.7 if("Count"inchobject) {8 //Execute Code9 };Ten One //bad wording: detect false values A if(object["Count"]) { - //do not execute code - }; the - //good wording. - if("Name"inchobject) { - //Execute Code + }; - + //bad syntax: detect if NULL A if(object["name"]! =NULL) { at //do not execute code -};
However, it is also important to note that the in operator also detects all properties on the prototype chain, and returns true whether it is on the instance or on the prototype of the inherited object that has the target attribute. If you need to detect whether a property exists for an object, you need to use the hasOwnProperty () method. All JS objects that inherit from object have this method and return True if the instance contains this property (False if the property exists only in the prototype).
However, in IE8 and earlier versions of IE, DOM objects are not inherited from object and do not contain the hasOwnProperty () method. When we use the hasOwnProperty () method, we should first detect if this method exists.
1 varObject = {2Count = 0,3Name =NULL4 };5 6 //This is the best implementation for all non-dom objects7 if(Object.hasownproperty ("name")) {8 //Execute Code9 }Ten One //This should be true for all objects that are not determined to be Dom A if("hasOwnProperty"inchObject && object.hasownproperty ("name")) { - //Execute Code -}
Regardless of the existence of a property under any circumstances, it is best to use the in operator or hasOwnProperty (), which avoids many bugs.
JavaScript Data detection