任意值、空值、Null和Undefined
- 任意值
不知道具体类型的变量,使用any标记为任意值;
Object作用类似,但是Object类型的变量,只允许赋任意值,不能调用它上面的方法,即便它真的有这些方法;
let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)
let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
-
空值
void类型像是与any类型相反,表示没有任何类型;
声明一个void类型的变量,只能赋予undefined和null,没有什么大用; -
null和undefined
null:主动释放一个变量引用的对象,表示一个变量不再指向任何对象地址,typeof返回object;undefined:是所有没有赋值变量的默认值,自动赋值,typeof返回undefined;
两者的值相等,类型不等;typeof返回操作数的数据类型;
instanceof判断对象是否为指定的类型;