数据类型判断

四种方法

typeof、instanceof、constructor、Object.prototype.toString.call()

1.typeof

console.log(
    typeof 100, //"number"
    typeof 'abc', //"string"
    typeof false, //"boolean"
    typeof undefined, //"undefined"
    typeof null, //"object"
    typeof [1,2,3], //"object"
    typeof {a:1,b:2,c:3}, //"object"
    typeof function(){console.log('aaa');}, //"function"
    typeof new Date(), //"object"
    typeof /^[a-zA-Z]{5,20}$/, //"object"
    typeof new Error() //"object"
    typeof new Number(100), //'object'
    typeof new String('abc'),// 'string'
    typeof new Boolean(true),//'boolean'
);

基本数据类型中:Number,String,Boolean,undefined 以及引用数据类型中Function ,可以使用typeof检测数据类型,分别返回对应的数据类型小写字符。
另:用typeof检测构造函数创建的Number,String,Boolean都返回object
基本数据类型中:null 。引用数据类型中的:Array,Object,Date,RegExp。不可以用typeof检测。都会返回小写的object

2.instanceof

console.log(
    100 instanceof Number, //false
    'dsfsf' instanceof String, //false
    false instanceof Boolean, //false
    undefined instanceof Object, //false
    null instanceof Object, //false
    [1,2,3] instanceof Array, //true
    {a:1,b:2,c:3} instanceof Object, //true
    function(){console.log('aaa');} instanceof Function, //true
    new Date() instanceof Date, //true
    /^[a-zA-Z]{5,20}$/ instanceof RegExp, //true
    new Error() instanceof Error //true
)

基本数据类型中:Number,String,Boolean。字面量值不可以用instanceof检测,但是构造函数创建的值可以,如下:

var num = new Number(123);
var str = new String('dsfsf');
var boolean = new Boolean(false)

还需要注意null和undefined都返回了false,这是因为它们的类型就是自己本身,并不是Object创建出来它们,所以返回了false。

3.constructor

constructor是prototype对象上的属性,指向构造函数。
根据实例对象寻找属性的顺序,若实例对象上没有实例属性或方法时,就去原型链上寻找,
因此,实例对象也是能使用constructor属性的。

如果输出一个类型的实例的constructor,就如下所示:

console.log(new Number(123).constructor)
//ƒ Number() { [native code] }

可以看到它指向了Number的构造函数,因此,可以使用num.constructor==Number来判断一个变量是不是Number类型的。

var num  = 123;
var str  = 'abcdef';
var bool = true;
var arr  = [1, 2, 3, 4];
var json = {name:'wenzi', age:25};
var func = function(){ console.log('this is function'); }
var und  = undefined;
var nul  = null;
var date = new Date();
var reg  = /^[a-zA-Z]{5,20}$/;
var error= new Error();

function Person(){
  
}
var tom = new Person();

// undefined和null没有constructor属性
console.log(
    tom.constructor==Person,
    num.constructor==Number,
    str.constructor==String,
    bool.constructor==Boolean,
    arr.constructor==Array,
    json.constructor==Object,
    func.constructor==Function,
    date.constructor==Date,
    reg.constructor==RegExp,
    error.constructor==Error
);
//所有结果均为true

除了undefined和null之外,其他类型都可以通过constructor属性来判断类型。

4.使用Object.prototype.toString.call()检测对象类型

可以通过toString() 来获取每个对象的类型。为了每个对象都能通过 Object.prototype.toString() 来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数,称为thisArg。

var toString = Object.prototype.toString;

toString.call(123); //"[object Number]"
toString.call('abcdef'); //"[object String]"
toString.call(true); //"[object Boolean]"
toString.call([1, 2, 3, 4]); //"[object Array]"
toString.call({name:'wenzi', age:25}); //"[object Object]"
toString.call(function(){ console.log('this is function'); });
 //"[object Function]"
toString.call(undefined); //"[object Undefined]"
toString.call(null); //"[object Null]"
toString.call(new Date()); //"[object Date]"
toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
toString.call(new Error()); //"[object Error]"

这样可以看到使用Object.prototype.toString.call()的方式来判断一个变量的类型是最准确的方法。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MIPS(Microprocessor without Interlocked Pipeline Stages)是一种经典的RISC(Reduced Instruction Set Computer)架构,用于设计和实现微处理器。在MIPS中,数据类型判断是通过指令集中的特定指令来完成的。 MIPS指令集提供了一些用于数据类型判断的指令,例如: - `BEQ`(Branch if Equal)指令用于比较两个寄存器中的值是否相等,并根据比较结果决定是否跳转到指定的目标地址。 - `BNE`(Branch if Not Equal)指令用于比较两个寄存器中的值是否不相等,并根据比较结果决定是否跳转到指定的目标地址。 - `SLT`(Set on Less Than)指令用于比较两个寄存器中的值是否满足小于关系,并将比较结果存储到目标寄存器中。 这些指令可以用于判断两个数据的大小关系或者是否相等。根据比较结果,可以执行不同的操作或者跳转到不同的代码块。 下面是一个示例,演示了如何使用MIPS指令进行数据类型判断: ```assembly .data num1: .word 10 num2: .word 20 .text main: lw $t0, num1 # 将num1加载到$t0寄存器 lw $t1, num2 # 将num2加载到$t1寄存器 beq $t0, $t1, equal # 如果$t0和$t1相等,则跳转到equal标签处 bne $t0, $t1, not_equal # 如果$t0和$t1不相等,则跳转到not_equal标签处 equal: # 相等时的操作 # ... j end # 跳转到程序结束处 not_equal: # 不相等时的操作 # ... end: # 程序结束 ``` 在上面的示例中,首先将`num1`和`num2`加载到寄存器中,然后使用`beq`和`bne`指令进行比较,根据比较结果跳转到不同的标签处执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值