js判断变量,对象类型的方法

  1. typeof
    typeof是一个运算符,有两种使用方式:typeof(表达式)和typeof 变量名,第一种是对表达式做运算,第二种是对变量做运算。
    typeof的返回值一般有如下几种类型:
    undefined,boolean,string,number,object(对象类型的变量或者值,或者null。null作为object类型处理),function
    typeof a //undefined
    typeof(true) //boolean
    typeof(‘123’) //string
    typeof(123) //number
    typeof NaN //number
    typeof null //object
    typeof(new String()) //object
    typeof(function(){}) //function
    typeof(class c{}) //function
    总结:
    typeof运算符用于判断对象的类型,但是对于一些创建的对象,都会返回object,有时需要判断该实例是否为某个对象的实例,那么这个时候需要用到instanceof
  2. instanceof
    instanceof运算符用来判断一个对象在其原型链中是否存在一个构造函数的prototype属性。
    判断一个变量是否某个对象的实例:
var a = new Array()
console.log(a instanceof Array)    //true
console.log(a instanceof Object)   //true
function test(){}
var fn = new test()
console.log(fn instanceof test)     //true
console.log(a==b)                   //false

instanceof可以在继承关系中用来判断一个实例是否属于它的父类型

function Foo(){}
Foo.prototype = new Aoo()
var foo = new Foo()
console.log(foo instanceof Foo)      //true
console.log(foo instanceof Aoo)      //true
//上边的代码中是判断了一层继承关系中的父类,在多层继承关系中,instanceof运算符同样适用
console.log(Object instanceof Object);//true 
console.log(Function instanceof Function);//true 
console.log(Number instanceof Number);//false 
console.log(String instanceof String);//false  
console.log(Function instanceof Object);//true  
console.log(Foo instanceof Function);//true 
console.log(Foo instanceof Foo);//false

3.constructor
对象的constructor属性用于返回创建该对象的函数,也就是我们常说的构造函数
在js中,每个具有原型的对象都会自动获得constructor属性。除了arguments,Enumerator,Error,Global,Math,RegExp,Regular Expression等一些特殊对象之外,其它所有的JavaScript内置对象都具有constructor属性。例如:Array,Bolean,Date,Function,Number,Object,String等。
语法: Object.constructor()
**返回值:**对象的constructor属性返回创建该对象的函数的引用

// 字符串:String()
var str = "张三";
alert(str.constructor); // function String() { [native code] }
alert(str.constructor === String); // true
 
// 数组:Array()
var arr = [1, 2, 3];
alert(arr.constructor); // function Array() { [native code] }
alert(arr.constructor === Array); // true
 
// 数字:Number()
var num = 5;
alert(num.constructor); // function Number() { [native code] }
alert(num.constructor === Number); // true
 
// 自定义对象:Person()
function Person(){
    this.name = "CodePlayer";
}
var p = new Person();
alert(p.constructor); // function Person(){ this.name = "CodePlayer"; }
alert(p.constructor === Person); // true
 
// JSON对象:Object()
var o = { "name" : "张三"};
alert(o.constructor); // function Object() { [native code] }
alert(o.constructor === Object); // true
 
// 自定义函数:Function()
function foo(){
    alert("CodePlayer");
}
alert(foo.constructor); // function Function() { [native code] }
alert(foo.constructor === Function); // true
 
// 函数的原型:bar()
function bar(){
    alert("CodePlayer");
}
alert(bar.prototype.constructor); // function bar(){ alert("CodePlayer"); }
alert(bar.prototype.constructor === bar); // true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值