针对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’