Home > Web Front-end > JS Tutorial > Several ways to determine data type in javascript

Several ways to determine data type in javascript

angryTom
Release: 2019-11-30 17:46:07
forward
3263 people have browsed it

Several ways to determine data type in javascript

When writing JavaScript code, you often need to determine the type of variables and literals. You can use typeof, instanceof, Array.isArray(), and other methods. Which one is the most convenient and most convenient? Practical and most worry-free? This article explores this issue.

1. typeof

1.1 Syntax

typeof returns a string representing an uncalculated operation Number type.

Syntax: typeof(operand) | typeof operand

Parameters: an expression representing an object or primitive value, the type of which will be returned

Description: typeof may return The values ​​are as follows:

[Related course recommendations: JavaScript Video Tutorial]

Type Result

Undefined“undefined”
Null“object”
Boolean“boolean”
Number“number”
Bigint“bigint”
String“string”
Symbol“symbol”
Copy after login

Host object (provided by the JS environment) Depends on the specific Implement

Function object “function”

Any other object “object”

From the definition and description point of view, this syntax can determine many data types, but if you look carefully , typeof null actually returns "object", which is confusing. I will introduce it in detail below. Let's take a look at this effect first:

// 数值
console.log(typeof 37) // number
console.log(typeof 3.14) // number
console.log(typeof(42)) // number
console.log(typeof Math.LN2) // number
console.log(typeof Infinity) // number
console.log(typeof NaN) // number 尽管它是Not-A-Number的缩写,实际NaN是数字计算得到的结果,或者将其他类型变量转化成数字失败的结果
console.log(Number(1)) //number Number(1)构造函数会把参数解析成字面量
console.log(typeof 42n) //bigint
// 字符串
console.log(typeof '') //string
console.log(typeof 'boo') // string
console.log(typeof `template literal`) // string
console.log(typeof '1') //string 内容为数字的字符串仍然是字符串
console.log(typeof(typeof 1)) //string,typeof总是返回一个字符串
console.log(typeof String(1)) //string String将任意值转换成字符串
// 布尔值
console.log(typeof true) // boolean
console.log(typeof false) // boolean
console.log(typeof Boolean(1)) // boolean Boolean会基于参数是真值还是虚值进行转换
console.log(typeof !!(1)) // boolean 两次调用!!操作想短语Boolean()
// Undefined
console.log(typeof undefined) // undefined
console.log(typeof declaredButUndefinedVariabl) // 未赋值的变量返回undefined
console.log(typeof undeclaredVariable ) // 未定义的变量返回undefined
// 对象
console.log(typeof {a: 1}) //object
console.log(typeof new Date()) //object
console.log(typeof /s/) // 正则表达式返回object
// 下面的例子令人迷惑,非常危险,没有用处,应避免使用,new操作符返回的实例都是对象
console.log(typeof new Boolean(true)) // object
console.log(typeof new Number(1)) // object
console.log(typeof new String('abc')) // object
// 函数
console.log(typeof function () {}) // function
console.log(typeof class C { }) // function
console.log(typeof Math.sin) // function
Copy after login

1.2 The mysterious null

## Since the birth of #javascript, typeof null has always returned 'object'. This is because the value in JavaScript consists of two parts, one part is the label representing the type, and the other part represents the actual value. The value type label of the object type is 0. Unfortunately, null represents a null pointer, and its type label is also designed to be 0, so there is this typeof null === 'object', the 'son of the devil'.

There was an ECMAScript proposal to allow typeof null to return 'null', but the proposal was rejected.

1.3 Use the new operator

The type of all constructors except Function is 'object', as follows:

var str = new String('String');    
var num = new Number(100)
console.log(typeof str) // object
console.log(typeof num) // object
var func = new Function()
console.log(typeof func) // function
Copy after login

1.4 The priority of the brackets in the grammar

typeof operation is higher than the " " operation, but lower than the parentheses

    var iData = 99
    console.log(typeof iData + ' Wisen') // number Wisen
    console.log(typeof (iData + 'Wisen')) // string
Copy after login

1.5 Determining the compatibility of regular expressions

typeof /s/ === 'function'; // Chrome 1-12 , 不符合 ECMAScript 5.1
typeof /s/ === 'object'; // Firefox 5+ , 符合 ECMAScript 5.1
Copy after login

1.6 Error

Before ECMAScript 2015, typeof was always guaranteed to return a string for any given operand, even if it was not declared. Without an assigned identifier, typeof can also return undefined, which means that using typeof will never report an error.

But after the block-level scope and let and const commands were added to ES6, using variables declared by let and const before the variable declaration will throw a ReferenceError error. Block-level scope variables are at the head of the block. There is a "temporary dead zone" between the beginning and the declaration of the variable. During this period, accessing the variable will throw an error. As follows:

    console.log(typeof undeclaredVariable) // 'undefined'
    console.log(typeof newLetVariable) // ReferenceError
    console.log(typeof newConstVariable) // ReferenceError
    console.log(typeof newClass) // ReferenceError
    let newLetVariable
    const newConstVariable = 'hello'
    class newClass{}
Copy after login

1.7 Exception

Currently all browsers expose a non-standard host object document.all of type undefined. typeof document.all === 'undefined'. The landscape specification allows custom type tags for non-standard foreign objects. It only requires that these type tags are different from existing ones. Examples of document.all type tags being undefined are classified in the web field as "intentional violations of the original ECMA JavaScript standard". Infringement" may be a browser prank.

Summary: typeof returns the type label of a variable or value. Although it can return correct results for most types, it is not ideal for null, constructor instances, and regular expressions.

2. instanceof

2.1 Syntax

The instanceof operator is used to detect the prototype chain of instance objects (parameters) Whether the prototype of the constructor appears.

Syntax: object instanceof constructor

Parameters: object an instance object

constructor a constructor function

Description: instanceof operator is used to detect constructor Whether .property exists on the prototype chain of parameter object.

    // 定义构造函数
    function C() {
    }    function D() {
    }    var o = new C()
    console.log(o instanceof C) //true,因为Object.getPrototypeOf(0) === C.prototype
    console.log(o instanceof D) //false,D.prototype不在o的原型链上
    console.log(o instanceof Object) //true 同上
    C.prototype = {}    var o2 = new C()
    console.log(o2 instanceof C) // true
    console.log(o instanceof C) // false C.prototype指向了一个空对象,这个空对象不在o的原型链上
    D.prototype = new C() // 继承
    var o3 = new D()
    console.log(o3 instanceof D) // true
    console.log(o3 instanceof C) // true C.prototype现在在o3的原型链上
Copy after login

It should be noted that if the expression obj instanceof Foo returns true, it does not mean that the expression will always return true. It should be that the value of the Foo.prototype attribute may be modified. The modified value It may not be on the prototype chain of obj, in which case the value of the expression is false. Another situation is to change the prototype chain of obj. Although in the current ES specification, the prototype of the object can only be read and cannot be modified, it can be modified with the help of the non-standard __proto__ pseudo-attribute, such as executing After obj.__proto__ = {}, obj instanceof Foo returns false. In addition, Object.setPrototypeOf() and Reflect.setPrototypeOf() in ES6 can modify the prototype of the object.

instanceof and multiple global objects (interaction between multiple iframes or multiple windows)

In browsers, javascript scripts may need to interact between multiple windows. Multiple windows mean multiple global environments, and different global environments have different global objects and thus different built-in constructors. This may cause some problems. For example, the expression [] instanceof window.frames[0].Array will return false because

Array.prototype !== window.frames[0].Array.prototype。
Copy after login

起初,这样可能没有意义,但是当在脚本中处理多个frame或多个window以及通过函数将对象从一个窗口传递到另一个窗口时,这就是一个非常有意义的话题。实际上,可以通过Array.isArray(myObj)或者Object.prototype.toString.call(myObj) = "[object Array]"来安全的检测传过来的对象是否是一个数组。

2.2 示例

String对象和Date对象都属于Object类型(它们都由Object派生出来)。

但是,使用对象文字符号创建的对象在这里是一个例外,虽然原型未定义,但是instanceof of Object返回true。

var simpleStr = "This is a simple string";
    var myString  = new String();
    var newStr    = new String("String created with constructor");
    var myDate    = new Date();
    var myObj     = {};
    var myNonObj  = Object.create(null);

    console.log(simpleStr instanceof String); // 返回 false,虽然String.prototype在simpleStr的原型链上,但是后者是字面量,不是对象
    console.log(myString  instanceof String); // 返回 true
    console.log(newStr    instanceof String); // 返回 true
    console.log(myString  instanceof Object); // 返回 true

    console.log(myObj instanceof Object);    // 返回 true, 尽管原型没有定义
    console.log(({})  instanceof Object);    // 返回 true, 同上
    console.log(myNonObj instanceof Object); // 返回 false, 一种创建非 Object 实例的对象的方法

    console.log(myString instanceof Date); //返回 false

    console.log( myDate instanceof Date);     // 返回 true
    console.log(myDate instanceof Object);   // 返回 true
    console.log(myDate instanceof String);   // 返回 false
Copy after login

注意:instanceof运算符的左边必须是一个对象,像"string" instanceof String,true instanceof Boolean这样的字面量都会返回false。

下面代码创建了一个类型Car,以及该类型的对象实例mycar,instanceof运算符表明了这个myca对象既属于Car类型,又属于Object类型。

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
var mycar = new Car("Honda", "Accord", 1998);
var a = mycar instanceof Car;    // 返回 true
var b = mycar instanceof Object; // 返回 true
Copy after login

不是...的实例

要检测对象不是某个构造函数的实例时,可以使用!运算符,例如if(!(mycar instanceof Car))

instanceof虽然能够判断出对象的类型,但是必须要求这个参数是一个对象,简单类型的变量,字面量就不行了,很显然,这在实际编码中也是不够实用。

总结:obj instanceof constructor虽然能判断出对象的原型链上是否有构造函数的原型,但是只能判断出对象类型变量,字面量是判断不出的。

3. Object.prototype.toString()

3.1. 语法

toString()方法返回一个表示该对象的字符串。

语法:obj.toString()

返回值:一个表示该对象的字符串

描述:每个对象都有一个toString()方法,该对象被表示为一个文本字符串时,或一个对象以预期的字符串方式引用时自动调用。默认情况下,toString()方法被每个Object对象继承,如果此方法在自定义对象中未被覆盖,toString()返回“[object type]”,其中type是对象的类型,看下面代码:

var o = new Object();
 console.log(o.toString()); // returns [object Object]
Copy after login

注意:如ECMAScript 5和随后的Errata中所定义,从javascript1.8.5开始,toString()调用null返回[object, Null],undefined返回[object Undefined]

3.2. 示例

覆盖默认的toString()方法

可以自定义一个方法,来覆盖默认的toString()方法,该toString()方法不能传入参数,并且必须返回一个字符串,自定义的toString()方法可以是任何我们需要的值,但如果带有相关的信息,将变得非常有用。

下面代码中定义Dog对象类型,并在构造函数原型上覆盖toString()方法,返回一个有实际意义的字符串,描述当前dog的姓名,颜色,性别,饲养员等信息。

function Dog(name,breed,color,sex) {
        this.name = name;
        this.breed = breed;        
        this.color = color;        
        this.sex = sex;
    }
    Dog.prototype.toString = function dogToString() {        
    return "Dog " + this.name + " is a " + this.sex + " " + this.color + " " + this.breed
    }    
    var theDog = new Dog("Gabby", "Lab", "chocolate", "female");
    console.log(theDog.toString()) //Dog Gabby is a female chocolate Lab
Copy after login

4. 使用toString()检测数据类型

目前来看toString()方法能够基本满足javascript数据类型的检测需求,可以通过toString()来检测每个对象的类型。为了每个对象都能通过Object.prototype.toString()来检测,需要以Function.prototype.call()或者Function.prototype.apply()的形式来检测,传入要检测的对象或变量作为第一个参数,返回一个字符串"[object type]"。

    // null undefined
    console.log(Object.prototype.toString.call(null)) //[object Null] 很给力
    console.log(Object.prototype.toString.call(undefined)) //[object Undefined] 很给力
    // Number
    console.log(Object.prototype.toString.call(Infinity)) //[object Number]
    console.log(Object.prototype.toString.call(Number.MAX_SAFE_INTEGER)) //[object Number]
    console.log(Object.prototype.toString.call(NaN)) //[object Number],NaN一般是数字运算得到的结果,返回Number还算可以接受
    console.log(Object.prototype.toString.call(1)) //[object Number]
    var n = 100
    console.log(Object.prototype.toString.call(n)) //[object Number]
    console.log(Object.prototype.toString.call(0)) // [object Number]
    console.log(Object.prototype.toString.call(Number(1))) //[object Number] 很给力
    console.log(Object.prototype.toString.call(new Number(1))) //[object Number] 很给力
    console.log(Object.prototype.toString.call('1')) //[object String]
    console.log(Object.prototype.toString.call(new String('2'))) // [object String]
    // Boolean
    console.log(Object.prototype.toString.call(true)) // [object Boolean]
    console.log(Object.prototype.toString.call(new Boolean(1))) //[object Boolean]
    // Array
    console.log(Object.prototype.toString.call(new Array(1))) // [object Array]
    console.log(Object.prototype.toString.call([])) // [object Array]
    // Object
    console.log(Object.prototype.toString.call(new Object())) // [object Object]
    function foo() {}
    let a = new foo()
    console.log(Object.prototype.toString.call(a)) // [object Object]
    // Function
    console.log(Object.prototype.toString.call(Math.floor)) //[object Function]
    console.log(Object.prototype.toString.call(foo)) //[object Function]
    // Symbol
    console.log(Object.prototype.toString.call(Symbol('222'))) //[object Symbol]
    // RegExp
    console.log(Object.prototype.toString.call(/sss/)) //[object RegExp]
Copy after login

上面的结果,除了NaN返回Number稍微有点差池之外其他的都返回了意料之中的结果,都能满足实际开发的需求,于是我们可以写一个通用的函数来检测变量,字面量的类型。如下:

    let Type = (function () {
        let type = {};
        let typeArr = [&#39;String&#39;, &#39;Object&#39;, &#39;Number&#39;, &#39;Array&#39;, &#39;Undefined&#39;, &#39;Function&#39;, &#39;Null&#39;, &#39;Symbol&#39;, &#39;Boolean&#39;, &#39;RegExp&#39;, &#39;BigInt&#39;];        for (let i = 0; i < typeArr.length; i++) {
            (function (name) {
                type[&#39;is&#39; + name] = function (obj) {                    return Object.prototype.toString.call(obj) === &#39;[object &#39; + name + &#39;]&#39;
                }
            })(typeArr[i])
        }        return type
    })()
    let s = true
    console.log(Type.isBoolean(s)) // true
    console.log(Type.isRegExp(/22/)) // true
Copy after login

除了能检测ECMAScript规定的八种数据类型(七种原始类型,Boolean,Null,Undefined,Number,BigInt,String,Symbol,一种复合类型Object)之外,还能检测出正则表达式RegExp,Function这两种类型,基本上能满足开发中的判断数据类型需求。

5. 判断相等

既然说道这里,不妨说一说另一个开发中常见的问题,判断一个变量是否等于一个值。ES5中比较两个值是否相等,可以使用相等运算符(==),严格相等运算符(===),但它们都有缺点,== 会将‘4’转换成4,后者NaN不等于自身,以及+0 !=== -0。ES6中提出”Same-value equality“(同值相等)算法,用来解决这个问题。Object.is就是部署这个算法的新方法,它用来比较两个值是否严格相等,与严格比较运算(===)行为基本一致。

    console.log(5 == &#39;5&#39;) // true
    console.log(NaN == NaN) // false
    console.log(+0 == -0) // true
    console.log({} == {}) // false
    console.log(5 === &#39;5&#39;) // false
    console.log(NaN === NaN) // false
    console.log(+0 === -0) // true
    console.log({} === {}) // false
Copy after login

Object.js()不同之处有两处,一是+0不等于-0,而是NaN等于自身,如下:

    let a = {}
    let b = {}
    let c = b
    console.log(a === b) // false
    console.log(b === c) // true
    console.log(Object.is(b, c)) // true
Copy after login

注意两个空对象不能判断相等,除非是将一个对象赋值给另外一个变量,对象类型的变量是一个指针,比较的也是这个指针,而不是对象内部属性,对象原型等。

本文来自 js教程 栏目,欢迎学习!  

The above is the detailed content of Several ways to determine data type in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template