变量

特点: 松散型类型

结果:

  1. 仅仅是一个用来保存值得占位符,可随时变换类型;
  2. 未经过初始化得变量,默认值 undefined;
  3. var定义的变量 将是该变量得作用域的局部变量,如函数中var定义的变量,在函数退出后 就会被销毁
  4. 不使用操作符,直接定义一个变量,此变量将为一个全局变量。
var msg = 123;
console.log(typeof msg) // "number"
msg = 'hello world';

console.log(msg);// 'hello world'
console.log(typeof msg)// "string"
var msg; 
console.log(msg === undefined) // true
function hello() { var message = 'hello';}
 hello();
console.log(message) // 报错:Uncaught ReferenceError: message is not defined
function test() {
     a = '4545'; // 定义一个全局变量a, 严格模式下也ReferenceError
   b
}
console.log(a) // VM503:1 Uncaught ReferenceError: a is not defined
console.log(b) // VM503:1 Uncaught ReferenceError: a is not defined
test()

console.log(a) // '4545'
console.log(b) // VM503:1 Uncaught ReferenceError: a is not defined

  

 

posted @ 2020-09-23 10:39  baixinL  阅读(94)  评论(0编辑  收藏  举报