The value null is written with a literal: null. null is not an identifier for a property of the global object, like undefined can be. Instead, null expresses a lack of identification, indicating that a variable points to no object. In APIs, null is often retrieved in a place where an object can be expected but no object is relevant.
let b=null表示b是一个不存在的对象,不存在的对象哪来的属性?所以报错
let b= {};这样b是一个空对象,b.s取空对象中不存在的属性才会输出underfind的。
你的需求本身就是反javascript的。。
这个应该实现不了,js就是这么设定的不能在 null上调用任何属性或方法
如果硬要实现
这个需要重写js引擎吧。。。
先说说对null取属性是为了什么??
有属性了。。。怎么还能叫null。。。
硬要做的话,再取属性之前做个判断,假如b是null就直接返回null
给个默认值?
The value null is written with a literal: null. null is not an identifier for a property of the global object, like undefined can be. Instead, null expresses a lack of identification, indicating that a variable points to no object. In APIs, null is often retrieved in a place where an object can be expected but no object is relevant.
用try catch吧。js的假值有点多,不过貌似就用if()判断已经比较优雅了。