在javascript中判断类型

栏目: JavaScript · 发布时间: 6年前

内容简介:一个字符串始终是一个字符串,所以这一块是很容易。除非使用new(new String)调用,否则typeof将返回“object”。所以也要包含那些可以使用的字符串instanceof。From typeof more things than just an ordinary number will return "number" like NaN and Infinity. To know if a value really is a number the function isFinite is als

一个字符串始终是一个字符串,所以这一块是很容易。除非使用new(new String)调用,否则typeof将返回“object”。所以也要包含那些可以使用的字符串instanceof。

// Returns if a value is a string
function isString (value) {
    return typeof value === 'string' || value instanceof String;
}
复制代码

Number

From typeof more things than just an ordinary number will return "number" like NaN and Infinity. To know if a value really is a number the function isFinite is also required. 从类型更多的东西,而不仅仅是普通的数字将返回像NaN和无限的“数字”。要知道值是否真的是数字,函数isFinite也是必需的。

// Returns if a value is really a number
function isNumber (value) {
    return typeof value === 'number' && isFinite(value);
}
复制代码

Array

在javascript 数组中不是像 java 和其他语言中那样的真正数组。它们实际上是对象,因此typeof将为它们返回“对象”。要知道某些东西是否真的是一个数组,它的构造函数可以与Array进行比较。

// Returns if a value is an array
function isArray (value) {
    return value && typeof value === 'object' && value.constructor === Array;
}

// ES5 actually has a method for this (ie9+)
Array.isArray(value);
复制代码

Function

// Returns if a value is a function
function isFunction (value) {
    return typeof value === 'function';
}
复制代码

Object

很多东西都是javascript中的对象。要知道值是否是可以具有属性并循环的对象,可以将其构造函数与Object进行比较。它不适用于从类创建的对象,因此可以使用instanceof运算符。

// Returns if a value is an object
function isObject (value) {
    return value && typeof value === 'object' && value.constructor === Object;
}
复制代码

Null & undefined

大多数情况下,您不需要显式检查null和undefined,因为它们都是假值。然而,要做到这一点,下面的功能就可以了。

// Returns if a value is null
function isNull (value) {
    return value === null;
}

// Returns if a value is undefined
function isUndefined (value) {
    return typeof value === 'undefined';
}
复制代码

Boolean

对于布尔值, typeof就足够了,因为它返回true和false的“boolean”。

// Returns if a value is a boolean
function isBoolean (value) {
    return typeof value === 'boolean';
}
复制代码

RegExp

RegExp是对象,因此唯一需要检查的是构造函数是否为RegExp。

// Returns if a value is a regexp
function isRegExp (value) {
    return value && typeof value === 'object' && value.constructor === RegExp;
}
复制代码

Error

javascript中的错误与许多其他编程语言中的“异常”相同。它们有几种不同的形式,例如Error,TypeError和RangeError。一个instanceof语句对他们来说已经足够了,但我们还要确保我们还检查错误所具有的“message”属性。

// Returns if value is an error object
function isError (value) {
    return value instanceof Error && typeof value.message !== 'undefined';
}
复制代码

Date

日期实际上不是javascript中的数据类型。但要知道是否有某个Date对象,可以使用instanceof进行检查。

// Returns if value is a date object
function isDate (value) {
    return value instanceof Date;
}
复制代码

Symbol

// Returns if a Symbol
function isSymbol (value) {
    return typeof value === 'symbol';
}
复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Designing Data-Intensive Applications

Designing Data-Intensive Applications

Martin Kleppmann / O'Reilly Media / 2017-4-2 / USD 44.99

Data is at the center of many challenges in system design today. Difficult issues need to be figured out, such as scalability, consistency, reliability, efficiency, and maintainability. In addition, w......一起来看看 《Designing Data-Intensive Applications》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

html转js在线工具
html转js在线工具

html转js在线工具