【问题标题】:Nested self executing function java script throwing error for console.log() [duplicate]console.log()的嵌套自执行函数javascript抛出错误[重复]
【发布时间】:2016-02-18 14:07:27
【问题描述】:

console.log() 的嵌套自执行函数 java 脚本抛出错误 https://jsfiddle.net/vivek7189/co5oeqcg/

(function(a,b){
  console.log("value a:"+a +" value b:"+b)
   (function (a,b){
    console.log("value a:"+a +" value b:"+b)
   })(20,10)
})(30,40)

【问题讨论】:

  • 有时,javascript 无法猜测在哪里为您放置分号,并弄错了...正确使用分号,这种痛苦就会消失...基本上 js 将其解释为console.log('...')( ... ) ...由于console.log返回“未定义”并且“未定义”不是一个函数,它失败了......你的代码中应该有4个; - 你没有
  • @JaromandaX 不需要4个;,只需要console.log()就够了..
  • 我从来没有说过代码“需要” 4,我说过 应该是 4 - 即使你添加了你说需要的两个,javascript“假装”另外两个是还有...哦,您可以通过添加单个; 来运行代码 - 或者通过*缩短代码1个字符

标签: javascript


【解决方案1】:

JavaScript 引擎处理

(function (a,b){
  console.log("value a:"+a +" value b:"+b);
})

作为函数调用的参数,因为引擎认为第一个 console.log("value a:"+a +" value b:"+b) 在之后将某些内容放入括号后返回一个函数,但它不是...... 您必须让引擎了解您不会尝试将第一个 console.log("value a:"+a +" value b:"+b) 的结果作为函数调用,例如通过在第一个 console.log("value a:"+a +" value b:"+b) 之后添加 ; 甚至 +(随便),以便引擎了解它的两个陈述而不是一个

【讨论】:

    【解决方案2】:

    ; 放入console.log()

    (function(a,b){
      console.log("value a:"+a +" value b:"+b);
      (function (a,b){
        console.log("value a:"+a +" value b:"+b);
      })(20,10)
    })(30,40)
    

    【讨论】:

      【解决方案3】:

      考虑这段代码

      function foo(a) {
          return function(b) {
              return a+b;
          }
      }
      

      你可以这样运行

      var x = foo(2);
      var y = x(3); // y == 5
      

      但这可以像“速记”一样完成

      var x = foo(2)(3); // x == 5
      

      在你的代码中,你有

      console.log("value a:"+a +" value b:"+b);
        (function (a,b){
          console.log("value a:"+a +" value b:"+b);
        })(20,10)
      

      没有分号,javscript 猜错了,认为你在尝试做

      console.log('')( ... )
      

      您可以通过几种方式使代码运行

      minimal - 在代码中添加单个字符

      (function(a,b){
          console.log("value a:"+a +" value b:"+b); // add this colon
          (function (a,b){
              console.log("value a:"+a +" value b:"+b)
          }(20,10))
      })(30,40)
      

      让你的代码更短

      (function(a,b){
          console.log("value a:"+a +" value b:"+b)
          // use different syntax for the inner IIFE 
          // add a leading !, or +, or various other characters
          !function (a,b){
              console.log("value a:"+a +" value b:"+b)
          }(20,10)
      })(30,40)
      

      但是,为避免任何陷阱,请将冒号放在应有的位置

      v----- this one is optional, but may be required depending on preceding code
      ;(function(a,b){
          console.log("value a:"+a +" value b:"+b); // here
          (function (a,b){
              console.log("value a:"+a +" value b:"+b) // here
          }(20,10)); // here
      })(30,40); // and here
      

      【讨论】:

        猜你喜欢
        • 2021-05-10
        • 1970-01-01
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多