js类型

62 阅读1分钟
  1. javascript里的变量没有类型,只有值才有类型,所以他是一门弱类型语言。在typescript中添加了类型指定,所以在定义变量之后,该变量名只能持有指定的变量类型,否则会报错。
  2. function和array都是object的子类型
  3. typeof undeclared安全防范
var a;
typeof a; // undefined
typeof b; // undefined
//undefined is not same as undeclared

b虽然是undeclared,但是typeof不报错;
这种机制对于js在浏览器中运行有帮助,

/**
*检查DEBUG是否被加载到浏览器
*/
    // Throw Error
    if(DEBUG){
        console.log('debug is starting');
    }
    // safety
    if(typeof DEBUG == 'undefined' ){
        console.log('debug is starting');
    }