函数参数默认值 递归 预编译 暗示全局作用域

本文探讨了JavaScript中的函数提升现象,递归实现阶乘,并剖析了预编译阶段的作用。通过实例讲解了变量提升和隐式类型转换,涉及关键概念如AO(ACTIVATION OBJECT)和GO(GLOBAL OBJECT)。
摘要由CSDN通过智能技术生成

<script type="text/javascript">
  //初始化参数 默认值:undefined
  function test(a=1,b){

      //var a=arguments[0] || 1;
      //var a=arguments[1] || 2;
      /*
      if (typeof arguments[0] !== 'undefined'){
          a=arguments[0];
      }else{
          a=1;
      }
       */

      console.log(a+b);
      console.log(a);
      console.log(b);//未给b赋值时输出为undefined
  }
  //test(undefined,2);//1,2     实参与形参谁不是undefined 取值就是谁 如果都是则为undefined 这也是一种映射关系

  //n的阶乘 不使用for循环 使用递归(调用自身)   fact() 规律:n!=n*(n-1) 出口
  function fact(n){
      if(n === 1){
          return 1;
      }
      return n * fact(n-1);
  }
 //console.log(fact(5));   //js慎用递归

  //*****************预编译**************************
  /*
  函数和变量都会先声明提升(即使在一个函数里面的if里面的一个变量也会预编译放在AO) 函数高于变量 赋值不会提升
  假设一个函数体里面有一个变量语句:b=3; 因为没有var 所以会预编译放到GO里面
  1.先通篇检查语法错误
  2.预编译的过程
  3.解释一行 执行一行
   */
  // test();
  // function t(){console.log("前后都可执行")}
  // test();

  //暗示全局变量  imply global variable
  //var a = 1;   a=window.a;  全局vat一个变量都会存储在window里面 同等的
  // 函数体里面 var a=b=1; b未经过声明而赋值 则会自动添加到window里面 所以外部可以调用到b的值

  //----AO activation object 活跃函数(函数上下文)
  console.log("-----------------------");
  function test1(a){
      console.log(a);//函数提升 function a(){}
      var a=1;
      console.log(a);//1
      function a(){};
      console.log(a);//***** 1
      var b=function (){};
      console.log(b);//function ()
      function d(){};
  }
  //test1(2);
  /*
  分析:
  AO{
    a:undefined,
    b:undefined
    d:function d(){}
    a: 2->f(){}->1
  }
   */

  //------GO global object 全局上下文--------
  /*
  1.找变量 2.找函数声明 3。执行
  GO === window
   */
  console.log(a,b);
  function a(){};
  var b=function (){};
  /*
  GO={
    b:undefined
    a:function(){}
    }
   */
  //------------练习理解-----------------
  var b=3;
  console.log(a);//function (a){...}
  function a(a){
      console.log(a);//f a(){}
      var a=2;
      console.log(a);//2
      function a(){
          var b=5;
          console.log(b);//5
      }
  }
 // a(1);
  /*
  分析:
  GO={
    b:undefined -> 3
    a:function a(){...}
  }
  AO={
    a:undefined -> 1 -> function(){} -> 2
    b:undefined -> 5
  }
   */
  //笔试题 隐式类型转换
  console.log("------------------------")
  var a=false+1;
  console.log(a);//隐式类型转换
  console.log("------------------------")
  var b = false == 1;
  console.log(b);
  console.log("------------------------")
  if (typeof (a) && (-true) + (+undefined) + ''){
      console.log("通过了");
  }else {
      console.log("没通过");
  }
  console.log('---------------');
  if (1+5*'3' === 16){
      console.log("通过了")
  }else {
      console.log("未通过")
  };
  console.log('---------------');
  console.log(!!' ' + !!'' - !!false || '未通过');
  console.log('---------------');
  window.a || (window.a='1');
  console.log(window.a);//1    ()优先级高于||  先赋值1 在看或运算
  
  
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值