实现说明
活动部件是函数 greet。
使用要点
- 跑完文件,输出里会留下标记——
assigned:、current:、null == undefined:。
常见问题
运行时需要 npm、pip、cargo 之类帮忙吗?
不需要——包管理器在这里无事可做,没有任何声明。 文件在加载时定义 greet。
签名一览:function greet(name, greeting = 'hello')
核心语句
greet('ada', undefined);
const current = null;
greet('ada', null);
代码示例
// undefined:声明了还没赋值,或者访问了不存在的属性
let assigned;
console.log('assigned:', assigned, '| typeof:', typeof assigned);
// null:程序显式写入的空值;typeof null 是历史遗留的 "object"
const current = null;
console.log('current:', current, '| typeof null:', typeof null);
// == 把两者视为等价,=== 区分;写空值判断时用 ===
console.log('null == undefined:', null == undefined);
console.log('null === undefined:', null === undefined);
// 默认参数只在收到 undefined 时生效:null 会原样传入
function greet(name, greeting = 'hello') {
console.log(greeting, name);
}
greet('ada', undefined);
greet('ada', null);
// 数字转换的结果不同:Number(null) 是 0,Number(undefined) 是 NaN
console.log('Number(null):', Number(null), '| Number(undefined):', Number(undefined));