2021-05-24

针对javascript的null和undefined 的思考

笔者现做了如下测试  之后说是一些思考和总结

 console.log(typeof Persion);
        //对null的理解
        console.log(typeof null);//object
        console.log(null instanceof(Object));//false
        console.log(p1 instanceof(Object));//true
        //undefined
        console.log(typeof undefined);//undefined
        console.log(undefined instanceof(Object));//false
        //console.log(undefined instanceof(undefined)); //提示报错 Right-hand side of 'instanceof' is not an object
        //console.log(undefined instanceof(null)); //提示报错 Right-hand side of 'instanceof' is not an object

null和undefined 都是JavaScript的基本数据类型

Object Array Function为js 的引用数据类型

令我唯一困惑的是:   

console.log(typeof null);//object

null的作用

1.初始化未定义数据的对象

2.对象指向null  引导垃圾回收机制回收

undifined 的作用

之生命变量但就是没有赋值  js解析器 无法判定变量的类型和内容   于是 typeof 是undefined

综上 判断obj是否为空  用obj===null

判断obj是否为undefined  用 typeof obj === ‘undefined’

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值