变量声明打印

    a = 2;
    var a
    console.log(a); // 2
    a = 2;
    var a = 1
    console.log(a); // 1
    a = 2;
    function a () {
    }
    console.log(a); // 2
    var a = 2;
    function a () {
    }
    console.log(a); // 2
    a = 2;
    function a () {
    }
    var a
    console.log(a); // 2
    const a = 2;
    function a () {
    }
    console.log(a); // 不予许重复声明
    var a = 2;
    a = function () {
    	console.log('====')
    }
    function a() {
    }
    console.log(a);
    // function () {
    //	console.log('====')
    // }
	console.log(a); // undefined
    var a = 2;
    console.log(a); // 2
	console.log(a); // 报错 Uncaught ReferenceError: a is not defined
    a = 2;
    console.log(a); // 阻塞不执行
	console.log(b); // 报错,es6 let/const 不存在变量提升
	let b = 2;
	x = "global";
	(function() {
	    console.log(x) // global
	}());
	x = "global";
	(function() {
	    console.log(x) // undefined
	    var x = 123;
	}());
	x = "global";
	(function() {
	    console.log(x) // 报错,es6 let/const 暂时性死区,声明的块级作用域内,凡是在声明之前就使用这些变量,就会报错
	    let x = 123;
	}());
	function bar(x = y, y = 2) {
	  return [x, y];
	}
	bar(); // 报错
	// 上面代码中,调用bar函数之所以报错(某些实现可能不报错),是因为参数x默认值等于另一个参数y,
	// 而此时y还没有声明,属于“死区”。如果y的默认值是x,就不会报错,因为此时x已经声明了
	function bar(x = 2, y = x) {
	  return [x, y];
	}
	bar(); // [2, 2]
	// 不能在函数内部重新声明参数。
	function func(arg) {
	  let arg;
	}
	func() // 报错
	
	function func(arg) {
	  {
	    let arg;
	  }
	}
	func() // 不报错
	var tmp = new Date();
	function f() {
	  console.log(tmp);
	  if (false) {
	    var tmp = 'hello world';
	  }
	}
	f(); // undefined
	var tmp = 123;
	function f() {
	  console.log(tmp);
	  if (false) {
	    let tmp = 'hello world';
	  }
	}
	f(); // 123
	var tmp = 123;
	function f() {
	  console.log(tmp);
	  if (true) {
	    let tmp = 'hello world';
	  }
	  console.log(tmp);
	}
	f(); // 123  123
      console.log(a) // undefined
      {
        var a = 1
      }
      console.log(a) // 1
      if (false){
        var a = 1
      }
      console.log(a) // undefined
      if (false){
        a = 1
      }
      console.log(a) // 报错
      if (false) {
        function f() {}
        var a = 1
      }
      console.log(a) // undefined
      console.log(f) // undefined
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值