1. /*
  2. * EC(G)
  3. * VO(G) / GO
  4. * a「window.a」
  5. *
  6. * 变量提升:
  7. * var a;
  8. */
  9. console.log(a); //undefined
  10. if (!('a' in window)) { //'a' in window===true
  11. var a = 13;
  12. }
  13. console.log(a); //undefined
  1. /!*
  2. * EC(G)
  3. * fn --> 0x000 {GO}
  4. * 变量提升: function fn; 声明
  5. *!/
  6. console.log(fn); //undefined
  7. // fn(); //Uncaught TypeError: fn is not a function
  8. if ('fn' in window) {
  9. /!*
  10. * EC(BLOCK)
  11. * fn --> 0x000 [[scope]]:EC(BLOCK)
  12. * 作用域链:<EC(BLOCK),EC(G)>
  13. * 变量提升:function fn(){...} 声明+定义
  14. *!/
  15. fn(); //“哈哈哈”
  16. function fn() {
  17. console.log('哈哈哈');
  18. } //把之前对fn的操作,同步给全局一份
  19. }
  20. fn(); //“哈哈哈”
  1. /!*
  2. * EC(G)
  3. * 变量提升:--
  4. *!/
  5. f = function () {return true;}; //window.f=function...
  6. g = function () {return false;}; //window.g=function...
  7. (function () {
  8. /!*
  9. * EC(AN)
  10. * g
  11. * 作用域链:<EC(AN),EC(G)>
  12. * 形参赋值:--
  13. * 变量提升:function g; 声明
  14. *!/
  15. // g() //报错:g is not a function
  16. if ([] == ![]) {
  17. /!*
  18. * EC(BLOCK)
  19. * g --> 0x000 [[scope]]:EC(BLOCK)
  20. * 作用域链:<EC(BLOCK),EC(AN)>
  21. * 变量提升:function g(){...} 声明+定义
  22. *!/
  23. f = function () {return false;} //修改了全局的f值
  24. function g() {return true;} //EC(AN)中的g --> 0x000
  25. }
  26. })();
  27. console.log(f()); //false
  28. console.log(g()); //false
  29. // []==![] => []==false => 0==0 => true
  1. function sum(a) { //es6中没有变量提升,但是有一个自我检测的一个机制,在代码自上而下执行前,会先进行检测,
  2. console.log(a); //经过了词法解析 所以报错
  3. let a = 100;
  4. console.log(a);
  5. }
  6. sum(200);
  1. /*
  2. * EC(G)
  3. * obj --> 0x001 => {a:'包子',b:'馒头',name:'珠峰'}
  4. * a = 'name'
  5. * fn --> 0x000 [[scope]]:EC(G)
  6. * 变量提升:var obj; var a; function fn(obj){...};
  7. */
  8. var obj = {
  9. a: '包子',
  10. b: '面条'
  11. };
  12. var a = 'name';
  13. function fn(obj) {
  14. /*
  15. * EC(FN)
  16. * obj --> 0x001
  17. * --> 0x002 => {1:1}
  18. * 作用域链:<EC(FN),EC(G)>
  19. * 形参赋值:obj=0x001
  20. */
  21. console.log(obj); //=>{a:'包子',b:'面条'}
  22. obj[a] = '珠峰'; //把变量a存储的值作为属性名去操作 obj['name']='珠峰'
  23. obj.b = '馒头';
  24. obj = {};
  25. obj[1] = 1;
  26. console.log(obj); //=>{1:1}
  27. }
  28. fn(obj); //fn(0x001)
  29. console.log(obj); //=>{a:'包子',b:'馒头',name:'珠峰'}
  1. var i = 5;
  2. function fn(i) {
  3. return function (n) {
  4. console.log(n + (++i));
  5. }
  6. }
  7. var f = fn(1);
  8. f(2);//4
  9. fn(3)(4);//8
  10. fn(5)(6);//12
  11. f(7);//10
  12. console.log(i);//5
  1. var i = 20;
  2. function fn() {
  3. i -= 2;
  4. var i = 10;
  5. return function (n) {
  6. console.log((++i) - n);
  7. }
  8. }
  9. var f = fn();
  10. f(1);//console.log(11-1)=>10
  11. f(2);//console.log(++11-2)=>12-2=>10
  12. fn()(3);//console.log(++10-3)=>11-3=>8
  13. fn()(4);//console.log(++10-4)=>11-4=>7
  14. f(5);//console.log(++12-5)=>13-5=>8
  15. console.log(i);//20
  1. let x = 1;
  2. function A(y){
  3. let x = 2;
  4. function B(z){
  5. console.log(x+y+z);
  6. }
  7. return B;
  8. }
  9. let C = A(2);
  10. C(3); //7 x=2 y=2 +z=3 2+2+3=7
  1. //EC(G)
  2. // VO(G)/Go
  3. // fn======>x001
  4. // x====>5
  5. // f======>X002
  6. // 代码提升
  7. // fn
  8. let x = 5;
  9. function fn(x) {
  10. return function(y) {
  11. console.log(y + (++x));
  12. }
  13. }
  14. let f = fn(6);
  15. //EC(Fn)
  16. // x===》6=》7
  17. // return X002;//{function(y){console.log(y+(++x))}}
  18. f(7); //14 执行 x002(7)
  19. fn(8)(9);//18
  20. //EC(Fn2)
  21. // x===>8 9
  22. // return x003;//{function(y){console.log(y+(++x))}}
  23. // EC(AN) X003(9)
  24. // Scope<EC(AN),EC(Fn2)>
  25. // y=====>9
  26. // consoloe.log(y+(++x)) 9+9=18
  27. f(10);//15
  28. //x002(10)=>
  29. // EC(f2)
  30. // Scope<EC(f2),EC(f1)>
  31. // AO
  32. // y====10 console.log(y+(++x); 10+8=18
  33. console.log(x);//5
  34. //=>如果去掉function fn(x)的x呢?
  1. let a=0,
  2. b=0;
  3. function A(a){
  4. A=function(b){
  5. alert(a+b++);
  6. };
  7. alert(a++);
  8. }
  9. A(1);//2
  10. A(2);//4

1-3.png