The method determination JavaScript data type

A: typeof

typeof is an operator that there are several values ​​(number, boolean, string, undefined, null, function, object, symbol)

console.log(typeof 1);  // number
console.log(typeof 1.232);  // number
console.log(typeof 111111111111111111111111111111); // number
console.log(typeof NaN);    // number

console.log(typeof true);   // boolean
console.log(typeof false);  // boolean

console.log(typeof ''); // string
console.log(typeof 'a');    // string
console.log(typeof 'hello');    // string
console.log(typeof "world!");   // string
console.log(typeof String('你好'));   // string

console.log(typeof undefined);  // undefined
console.log(typeof null);   // object

console.log(typeof {}); // object
console.log(typeof {name: 'Tom', age: 23}); // object

console.log(typeof []); // object
console.log(typeof [1, 2, 3]);  // object
console.log(typeof function say(){});   // function
console.log(typeof function Person(name, age) {
    this.name = name;
    this.age = age;
    this.say = function () {
        console.log('hello, my name is ' + this.name + ', and i am ' + this.age + ' years old.');
    };
}); // function

console.log(typeof Symbol); // function
console.log(typeof Symbol('name')); // symbol
console.log(typeof new Set());  // object
console.log(typeof new Map());  // object
console.log(typeof new WeakSet());  // object
console.log(typeof new WeakMap());  // object
function Person(name, age) {
    this.name = name;
    this.age = age;
}

let p = new Person('Tom', 23);
console.log(typeof p);  // object

As can be seen from the above results, typeof operator only basic types number, boolean, string, undefined, symbol and function type function, other objects, such as null, an array, literal object constructor creates the object are determined as , can not determine the specific type.

 

Two: instanceof operator

console.log(1 instanceof Number);  // false
console.log(1.232 instanceof Number);  // false
console.log(111111111111111111111111111111 instanceof Number); // false
//console.log(NaN instanceof NaN);    // TypeError: Right-hand side of 'instanceof' is not an object
console.log(NaN instanceof Number);  // false
console.log('1=================================');

console.log(true instanceof Boolean);   // false
console.log(false instanceof Boolean);  // false
console.log('2=================================');

console.log('' instanceof String); // false
console.log('a' instanceof String);    // false
console.log('hello' instanceof String);    // false
console.log("world!" instanceof String);   // false
console.log(String('你好') instanceof String);   // false
console.log('3=================================');

// console.log(undefined instanceof undefined);  // TypeError: Right-hand side of 'instanceof' is not an object
console.log(undefined instanceof Number);   // false
console.log(undefined instanceof Object);   // false
// console.log(null instanceof null);   // TypeError: Right-hand side of 'instanceof' is not an object
console.log(null instanceof Object);    // false
console.log('4=================================');

console.log({} instanceof Object); // true
console.log({name: 'Tom', age: 23} instanceof Object); // true
console.log('5=================================');

console.log([] instanceof Array); // true
console.log([1, 2, 3] instanceof Array);  // true
console.log(new Array(4) instanceof Array);    // true
console.log('6=================================');

console.log(function say(){} instanceof Function);   // true
console.log(function Person(name, age) {
    this.name = name;
    this.age = age;
    this.say = function () {
        console.log('hello, my name is ' + this.name + ', and i am ' + this.age + ' years old.');
    };
} instanceof Function); // true
console.log(Symbol instanceof Function); // true
console.log('7=================================');

console.log(Symbol('name') instanceof Symbol); // false
console.log(new Set() instanceof Set);  // true
console.log(new Map() instanceof Map);  // true
console.log(new WeakSet() instanceof WeakSet);  // true
console.log(new WeakMap() instanceof WeakMap);  // true
console.log(new Date() instanceof Date);  // true
console.log(new RegExp(/he/) instanceof RegExp);    // true
function Person(name, age) {
    this.name = name;
    this.age = age;
}

let p = new Person('Tom', 23);
console.log(p instanceof Person);  // true

Can be drawn from the test results, the right operator must instanceof object, but only instanceof operator determines whether a prototype object constructor prototype chain present on the object to be detected (i.e., whether a specific enough to be detected examples of early function).

 

3: Use Object.prototype.toString.call (detected value)

This method takes the object's Class property value, the return value is a fixed format: [object Class Properties]

function myTypeof(obj) {
    let s = Object.prototype.toString.call(obj);
    return s.match(/\[object\s(\w*)\]/)[1].toLowerCase();
}
console.log(myTypeof(1));  // number
console.log(myTypeof(1.232));  // number
console.log(myTypeof(111111111111111111111111111111)); // number
console.log(myTypeof(NaN));    // number

console.log(myTypeof(true));   // boolean
console.log(myTypeof(false));  // boolean

console.log(myTypeof('')); // string
console.log(myTypeof('a'));    // string
console.log(myTypeof('hello'));    // string
console.log(myTypeof("world!"));   // string
console.log(myTypeof(String('你好')));   // string

console.log(myTypeof(undefined));  // undefined
console.log(myTypeof(null));   // null

console.log(myTypeof({})); // object
console.log(myTypeof({name: 'Tom', age: 23})); // object

console.log(myTypeof([])); // array
console.log(myTypeof([1, 2, 3]));  // array
console.log(myTypeof(new Array(4)));    // array

console.log(myTypeof(function say(){}));   // function
console.log(myTypeof(function Person(name, age) {
    this.name = name;
    this.age = age;
    this.say = function () {
        console.log('hello, my name is ' + this.name + ', and i am ' + this.age + ' years old.');
    };
})); // function
console.log(myTypeof(Symbol)); // function

console.log(myTypeof(Symbol('name'))); // symbol
console.log(myTypeof(new Set()));  // set
console.log(myTypeof(new Map()));  // map
console.log(myTypeof(new WeakSet()));  // weakset
console.log(myTypeof(new WeakMap()));  // weakmap
console.log(myTypeof(new Date()));  // date
console.log(myTypeof(new RegExp(/he/)));    // regexp
function Person(name, age) {
    this.name = name;
    this.age = age;
}

let p = new Person('Tom', 23);
console.log(myTypeof(p));  // object

Can be seen from the test results, the method can detect the secondary base type, function, array, js native object type, but can not be detected custom type.

 

4: Use the constructor property

constructor property returns create the constructor of the object references. With this feature, we can determine the type of the object, wherein the null and undefined because no constructor, the null and undefined require special handling.

function myTypeof(obj) {
    return obj === undefined ? 'undefined' :
        (obj === null ? 'null' :
            obj.constructor.toString().match(/function\s*([^(]*)/)[1].toLowerCase());
}

console.log(myTypeof(1));  // number
console.log(myTypeof(1.232));  // number
console.log(myTypeof(111111111111111111111111111111)); // number
console.log(myTypeof(NaN));    // number

console.log(myTypeof(true));   // boolean
console.log(myTypeof(false));  // boolean

console.log(myTypeof('')); // string
console.log(myTypeof('a'));    // string
console.log(myTypeof('hello'));    // string
console.log(myTypeof("world!"));   // string
console.log(myTypeof(String('你好')));   // string

console.log(myTypeof(undefined));  // undefined
console.log(myTypeof(null));   // null

console.log(myTypeof({})); // object
console.log(myTypeof({name: 'Tom', age: 23})); // object

console.log(myTypeof([])); // array
console.log(myTypeof([1, 2, 3]));  // array
console.log(myTypeof(new Array(4)));    // array

console.log(myTypeof(function say(){}));   // function
console.log(myTypeof(function Person(name, age) {
    this.name = name;
    this.age = age;
    this.say = function () {
        console.log('hello, my name is ' + this.name + ', and i am ' + this.age + ' years old.');
    };
})); // function
console.log(myTypeof(Symbol)); // function

console.log(myTypeof(Symbol('name'))); // symbol
console.log(myTypeof(new Set()));  // set
console.log(myTypeof(new Map()));  // map
console.log(myTypeof(new WeakSet()));  // weakset
console.log(myTypeof(new WeakMap()));  // weakmap
console.log(myTypeof(new Date()));  // date
console.log(myTypeof(new RegExp(/he/)));    // regexp
function Person(name, age) {
    this.name = name;
    this.age = age;
}

let p = new Person('Tom', 23);
console.log(myTypeof(p));  // person

Obtained from the detection result by the constructor property can determine all types except the null and undefined.

 

Guess you like

Origin www.cnblogs.com/yingtoumao/p/11519376.html