JS数据类型

六种数据类型:

1.number

2.string

3.boolean

4.null

5.undefined

6.object


检查数据类型的办法:

1.typeof xxx

var a = 123;
typeof a ; //number
var a = "123";
typeof a;  //string
var a = false;
typeof a;  //boolean
var a = null;
typeof a;  //object (null是空对象引用或者说指针)
var a;
typeof a; //undefined
var a = document.body;
typeof a ; //object

2.instanceof

 instanceof 是判断对象是已知数据类型的办法

var a = []  //创建一个new Array
a instanceof Array //true
var a = function(){}
a instanceof Function //true

注意:instanceof 后面一定是数据类型,并且大小写不能错,该方法适合一些条件或分支

3.constructor

var a = function(){};
a.constructor === Function //true

constructor不能类继承

function a(){};
function b(){};
a.prototype = new b();//继承b属性
var c = new a();
console.log(a);    //查看属性
console.log(c);
c.constructor === a;  //false

解决办法:constructor指向其本身

function a(){};
function b(){};
a.prototype = new b();//继承b属性
var c = new a();
console.log(a.constructor);    //ƒ Function() { [native code] }
console.log(c.constructor);    //ƒ b(){}
c.constructor = a;    //将自己的类赋值给对象的constructor属性
c.constructor === a;  //true

4.原型(prototype)


var a = []; //new Array()
Object.prototype.toString.call(a) === '[object Array]' //true
var a = ""; //new string()
Object.prototype.toString.call(a) === '[object String]' //true
var a = 123; //new number()
Object.prototype.toString.call(a) === '[object Number]' //true

通用性比较好

参考链接:http://blog.sina.com.cn/s/blog_51048da70101grz6.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值