创建方式
var a = null
// 当通过 id 获取 DOM 元素不存在时
var form = document.getElementById('#form'); // null
// 但通过 class 和 tagName 获取不存在时
var form = document.getElementsByTagName('form'); // HTMLCollection [] 空数组
使用场景
- 初始化值
var a = null
- 释放内存
for(var i=0;i<10;i++){
// do something
}
i = null
特性
null == undefined ; // true
null == false ; // false
null == 0 ; // false
null == ''; // false
typeof null === 'object'; // true
使用注意事项
// ES6 默认参数时
function test(a=1,b=2){
console.log(a,b);
}
// 常规默认选型
function test2(a,b){
a = a || 1
b = b || 2
console.log(a,b);
}
test(null,null); // null null
test(null,undefined); // null 2
test2(null,null); // 1 2
test2(null,undefined); // 1 2