Home >Web Front-end >JS Tutorial >Introduction to four methods of type detection in js (code)

Introduction to four methods of type detection in js (code)

不言
不言Original
2018-08-15 16:07:261145browse

This article brings you an introduction (code) about the four methods of type detection in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The order from the most rubbish method to the most awesome method is: typeof --> constructor --> instanceof --> toString

1.typeof

The detection object type is too vague. Function, object, and array types will all return object, so this method is garbage, but it is very practical and has a strong aura

2.constructor

The constructor of the instance object (instance object.constructor), returns the constructor, and the type can be distinguished

var str = 'abc';
        var num = 100;
        var arr = new Array();
        var date = new Date();
        alert(str.constructor);
        alert(num.constructor);
        alert(arr.constructor);
        alert(date.constructor);

3.instanceof

Determine whether an object is a constructor (class) Example. Note that this method can only detect instance objects. Returns a Boolean value

        var str=new String('abc');
        var num=new Number(100);
        var arr=new Array();
        var date=new Date();
        alert(str instanceof String);
        alert(num instanceof Number);
        alert(arr instanceof Array);
        alert(date instanceof Date);
        alert(str instanceof Object);

4.toString()

The most awesome five-star method. This method is powerful. It can convert hexadecimal and string. It is easy to use. High grid

        console.log(Object.prototype.toString.call(5).slice(8,-1));
        console.log(Object.prototype.toString.call('abc').slice(8,-1));
        console.log(Object.prototype.toString.call(true).slice(8,-1));
        console.log(Object.prototype.toString.call(function(){}).slice(8,-1));
        console.log(Object.prototype.toString.call([]).slice(8,-1));
        console.log(Object.prototype.toString.call({}).slice(8,-1));
        console.log(Object.prototype.toString.call(/\d/g).slice(8,-1));
        console.log(Object.prototype.toString.call(new Date).slice(8,-1));
        console.log(Object.prototype.toString.call(null).slice(8,-1));
        console.log(Object.prototype.toString.call(undefined).slice(8,-1));
        console.log(Object.prototype.toString.call(Math).slice(8,-1));
        // Number
        // String
        // Boolean
        // Function
        // Array
        // Object
        // RegExp
        // Date
        // Null
        // Undefined
        // Math

Related recommendations:

4 methods for js data type detection

Javascript isArray array type detection Function_javascript skills

Summary of data type detection methods in JavaScript

The above is the detailed content of Introduction to four methods of type detection in js (code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn