+ js basic data types data type determination method

Summary: No matter what type of, Object.prototype.toString.call (); can determine the specific type of simple basic types (String, Number, Boolean, Null, Undefined) are not objects, complex objects are the basic types subtype, the function is a special sub-type of the object (callable)

Data types include the basic types and reference types:

Basic Type : String, Number, Boolean, Null , Undefined, symbol (ES6)

引用类型:Object、Array、Date、Function、Error、RegExp、Math、Number、String、Boolean、Globle。

js built-in types have seven kinds : String, Number, Boolean, Null , Undefined, Symbol (ES6), Object

Analyzing data type methods generally be prepared by: typeof, instanceof, constructor, Object.prototype.toString.call (); four commonly used method

1, typeof :( can make accurate judgments about the basic types (including function), but for reference types, using it a bit powerless)

typeof Returns a string representation of the data type, the return result includes: number, boolean, string, object, undefined, function, Symbol6 data types.

For reference types, the returned object is, in fact, returned object is not wrong, because all objects in the prototype chain eventually all point to the Object, Object is the ancestor of all objects ``. But we need to know the specific type of an object, typeof becomes bloated up.

Note: typeof null object is returned, this is different because of a bug in the underlying objects are stored in binary, js is the first three binary 0 if the object is determined as the type, the null is binary 0, misjudge .

2, instanceof ( determines whether the instance of a class )

Objects and determine whether there is a constructor in the prototype chain relationship , if any relationship, returns true, otherwise returns false

Aaa function () { 
} 
var new new Aaa a1 = (); 
the console.log (a1 Aaa the instanceof); // a1 and Analyzing Aaa to true if a prototype in the same chain, is then returned true, false otherwise 
var arr = [ ]; 
the console.log (ARR the instanceof Aaa); // to false

We look

    var str = 'hello';
    alert(str instanceof String);//false
    var bool = true;
    alert(bool instanceof Boolean);//false
    var num = 123;
    alert(num instanceof Number);//false
    var nul = null;
    alert(nul instanceof Object);//false
    var und = undefined;
    alert(und instanceof Object);//false
    var oDate = new Date();
    alert(oDate instanceof Date);//true
    var json = {};
    alert(json instanceof Object);//true
    var arr = [];
    alert(arr instanceof Array);//true
    var reg = /a/;
    alert(reg instanceof RegExp);//true
    var fun = function(){};
    alert(fun instanceof Function);//true
    var error = new Error();
    alert(error instanceof Error);//true

We can see from the above operating results, the basic data type is not detected their type, but we use the following way to create num, str, boolean, can detect the type of:

var num = new Number(123);
var str = new String('abcdef');
var boolean = new Boolean(true);
console.log(num instanceof Number)
console.log(num instanceof String)

3, constructor: view corresponding to the object constructor

constructor below its corresponding prototype object is generated automatically. When we write a constructor, the program will automatically add: constructor name .prototype.constructor = constructor name

Aaa function () { 
} 
//Aaa.prototype.constructor = Aaa; // would have for each function, are generated automatically 
 
//Aaa.prototype.constructor = Aaa;

Analyzing data type methods

    var str = 'hello';
    alert(str.constructor == String);//true
    var bool = true;
    alert(bool.constructor == Boolean);//true
    var num = 123;
    alert(num.constructor ==Number);//true
   // var nul = null;
   // alert(nul.constructor == Object);//报错
    //var und = undefined;
    //alert(und.constructor == Object);//报错
    var oDate = new Date();
    alert(oDate.constructor == Date);//true
    var json = {};
    alert(json.constructor == Object);//true
    var arr = [];
    alert(arr.constructor == Array);//true
    var reg = /a/;
    alert(reg.constructor == RegExp);//true
    var fun = function(){};
    alert(fun.constructor ==Function);//true
    var error = new Error();
    alert(error.constructor == Error);//true

We can see from the above tests, undefined and null is not able to determine the type and error. Because null and undefined are invalid object , so there will not exist constructor
and we also need to be aware of are: the use of constructor is not insurance, because the constructor property can be modified, can lead to incorrect results detected

Aaa function () { 
} 
Aaa.prototype.constructor = Aaa; // program can automatically add, when we write the constructor, the program will automatically add this code 
function the BBB () {} 
Aaa.prototype.constructor the BBB = ; // At this point we modify the problem Aaa constructor 
alert (Aaa.construtor == Aaa); // false

As can be seen, constructor does not properly detect the correct constructor

Note: Use Object.create () js object is created, no constructor

do not have the null and undefined form of construction, and Date, only the corresponding structure in the form of both text form and not the contrary

4, Object.prototype.toString (it can be said that no matter what type, it can be judged immediately)

Principle is simple: a subtype of the object within the borrowed toString () method

toString is a method on the Object prototype object, the method returns to its default specific type of caller, more strictly speaking, is toString runtime object pointed to this type, type of return

Format [object xxx], xxx is the specific data types, including:

String, Number, Boolean, Undefined, Null, Function, Date, Array, RegExp, Error, HTMLDocument, ... basically all types of objects can be obtained by this method. 

    var str = 'hello';
    console.log(Object.prototype.toString.call(str));//[object String]
    var bool = true;
    console.log(Object.prototype.toString.call(bool))//[object Boolean]
    var num = 123;
    console.log(Object.prototype.toString.call(num));//[object Number]
    var nul = null;
    console.log(Object.prototype.toString.call(nul));//[object Null]
    var und = undefined;
    console.log(Object.prototype.toString.call(und));//[object Undefined]
    var oDate = new Date();
    console.log(Object.prototype.toString.call(oDate));//[object Date]
    var json = {};
    console.log(Object.prototype.toString.call(json));//[object Object]
    var arr = [];
    console.log(Object.prototype.toString.call(arr));//[object Array]
    var reg = /a/;
    console.log(Object.prototype.toString.call(reg));//[object RegExp]
    var fun = function(){};
    console.log(Object.prototype.toString.call(fun));//[object Function]
    var error = new Error();
    console.log(Object.prototype.toString.call(error));//[object Error]

This can also be seen from the results, no matter what type of, Object.prototype.toString.call (); it can be judged that specific type.
Next, we analyze the advantages and disadvantages of four methods

 

From the table we see, instanceof and constructor can not cross iframe, above did not elaborate, so let's direct the example myself

Example: Cross-page to determine whether an array

window.onload = function(){
    
    var oF = document.createElement('iframe');
    document.body.appendChild( oF );
    
    var ifArray = window.frames[0].Array;
    
    var arr = new ifArray();
    
    //alert( arr.constructor == Array );  //false
    
    //alert( arr instanceof Array );  //false
    
    alert( Object.prototype.toString.call(arr) == '[object Array]' );  //true
    
    
};

As can be seen from the results, no proper constructor and instanceof determines the type, only object.prototype.toString.call (); correctly determine the

In fact, the interviewer often prefer to have said that one of the easiest method is to determine the array, remember myself is object.prototype.toString.call () Oh!

Guess you like

Origin www.cnblogs.com/wangtong111/p/11286925.html