javaScript 变量提升,练习总结

f1();//20

var f1 = function () {
  console.log(10);
}

function f1() {
  console.log(20);
}

f1();
/* 执行后输出(运行)
20
10
*/
错误:
变量为undefined 使用函数
推测;先变量提升,函数后提升

var x = 30;
function test() {
  console.log(x);
  var x = 10;
  console.log(x);
  x = 20;
  function x() { };
  console.log(x);
}
test()
/* 执行后输出(运行)
f x(){}
10
20
*/
变量提升时,同名函数和变量问题
推测:变量在代码中向上找不到时,去上下文栈中输出距离更近的变量
a = 100
function demo(e) {
  function e() { }
  arguments[0] = 2;
  console.log(e)

  if (a > 10) {
    var b = 123;
    function c() { }
  }

  var c;
  a = 10
  var a;
  f = 123;
  console.log(b)
  console.log(c)
  console.log(a)
}
var a;
demo(1)
console.log(a)
console.log(f)
/* 执行后输出(运行)
2
undefined
undefined
10
100
123
*/
错误:f变量赋值之后可以不用声明,直接调用f其默认为全局对象
var count = 10;
function add() {
  var count = 0;
  return function () {
    count += 1;
    console.log(count)
  }
}
var s = add()
s();
s();
/* 执行后输出(运行)
1
2
*/
注意:如果一直使用 add()函数,每使用一次必须清除一次内存
防止内存泄漏,add()函数执行后创建的count不会在函数执行后自动清除而是不在引用
var person = function () {
  var name = "default";
  return {
    getName: function () {
      return name;
    },
    setName: function (newName) {
      name = newName;
    }
  }
}();
console.log(person.name);
console.log(person.getName());
person.setName("a");
console.log(person.getName());
/* 执行后输出(运行)
undefined
default
a
*/
错误:如果输出对象中没有的值,返回undefined
function test(a, b) {
  console.log(a);
  console.log(b);
  c = 0;
  var c;
  a = 3;
  b = 2;
  console.log(b);
  function b() { };
  function d() { };
  console.log(b);
  console.log(d);
}
test(1);
/* 执行后输出(运行)
1
f b(){}
2
2
f d(){}
*/
为什么b赋值后输出值而不是函数
推测:b赋值后b距离更近,不会再调用上下文栈函数b()
var x = 30;
function test1() {
  console.log(x);
  var x = 10;
  console.log(x);
  x = 20;
  function x() { };
  console.log(x);
}
test1()
/* 执行后输出(运行)
f x(){}
10
20
*/
验证推论正确
function test(a, b) {
  console.log(a);
  var c = 123;
  console.log(c);
  function a() { };
  console.log(b);
  var b = function c() { };
  console.log(b);
}
test(1, 3);
/* 执行后输出(运行)
f a(){}
123
3
f c(){}
*/
验证结果错误,以为加入函数上下文栈后变量a更接近,粗心大意
function a() {
  var b = 10;
  function c() {
    var b = 123;
    console.log(b);
    function d() { }
  }
  c();
  console.log(b);
  console.log(c);
}
var b = 123;
a();
/* 执行后输出(运行)
123
10
f c(){}

*/
验证推论正确
var a;
if (true) {
  console.log("1==", a, window.a);
  a = 5;
  console.log("2==", a, window.a);
  function a() {};
  console.log("3==", a, window.a);
  a = 0;
  console.log("4==", a, window.a);
}
console.log("5==", a, window.a);
/* 执行后输出(运行)
1==f a(){} undefined
2== 5 undefined
3== 5 5
1== 0 5
1== 5 5

*/
验证推论错误:为全局a赋值后,为什么第二次输出里面window.a输出结果不同,
推测:上下文栈中a和window对象中a传值问题,a赋值后第二次输出window.a才会显示
window.a值改变后必须对window.a赋值才能改变?并且对a的直接影响优先高于对a赋值

考虑对象性质:......不明白

变更提升最终总结:


1.先变量提升,函数后提升

2.变量在代码中向上找不到时,去上下文栈中输出距离更近的变量

3.当前以及上级作用域中找不到的变量,此变量赋值之后可以不用声明,直接调用时其默认为全局对象

4.如果输出对象中没有的值,返回undefined

5.b赋值后b在栈中距离更近,不会再调用上下文栈函数b()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值