错误的函数声明全局规则部分发出时,函数声明后没有分号?

| 这是两个规则集,它们说明了完全意外的行为。这是一个错误吗? 即使未在任何地方调用函数或由于JavaScript错误导致脚本中断,函数也会执行。解决方案是始终以这样的分号结束声明:
test1 = function(){console.log(\"test1\");}  ;// <==
但这很危险,因为这在javascript中不是必需的。 示例1小书签:
javascript:(function(){
  var d=document;var s=d.createElement(\'script\');s.text=&quot;
  KOBJ_config={\'rids\':[\'a1135x27\']};&quot;;d.body.appendChild(s);
  var l=d.createElement(\'script\');l.src=\'http://init.kobj.net/js/shared/kobj-static.js\';
  d.body.appendChild(l);})()
示例2小书签:
javascript:(function(){
  var d=document;var s=d.createElement(\'script\');s.text=&quot;
  KOBJ_config={\'rids\':[\'a1135x27\']};&quot;;d.body.appendChild(s);
  var l=d.createElement(\'script\');l.src=\'http://init.kobj.net/js/shared/kobj-static.js\';
  d.body.appendChild(l);})()
规则集:
ruleset a1135x27 {
  meta {
    name \"odd1\"
    description <<
        demonstrate oddity 1
    >>
    author \"Loïc Devaux\"
    logging on
  }

  dispatch {
    // Some example dispatch domains
    // domain \"example.com\"
    // domain \"other.example.com\"
  }

  global {

    emit  <|



       /* this breaks javascript code*/
       // => 16:27:58 ERROR general Closure Executed with error a1135x27 - 13025536774875610960847698152Exception: test1 is not defined
       test1 = function(){console.log(\"test1\")}

        /* this won\'t break */
        //test1 = function(){console.log(\"test1\")};
        /* */

       /* this won\'t break */
       //test1 = function(){console.log(\"test1\")}
       //test2 = function(){console.log(\"test2\")};
       /* */

    |>;
  }

  rule first_rule {
    select when pageview \".*\" setting ()
    pre { 

        }
    {
      emit <|

           test1();


          |>;  
    }

  }




}



ruleset a1135x28 {
  meta {
    name \"odd2\"
    description <<
        demonstrate oddity 2
    >>
    author \"Loic Devaux\"
    logging on
  }

 dispatch {
    // Some example dispatch domains
    // domain \"example.com\"
    // domain \"other.example.com\"
  }

  global {

    emit  <|

       /*  test1 will be executed even though it hasn\'t been called anywhere */
       //test1 = function(){console.log(\"test1\");}
        /* */

        /* test1 won\'t get executed */
        /* 
            test1 = function(){console.log(\"test1\");};
        */
        /* */


        /* this won\'t break and test2 will be executed although it hasn\'t been called anywhere */

            test1 = function(){console.log(\"test1\");}
            test2 = function(){console.log(\"test2\");}

        /* */


    |>;
  }

  rule first_rule {
    select when pageview \".*\" setting ()
    pre { 

        }
    {
      emit <|

          console.log(\'running first_rule\');

          |>;  
    }

  }




}
    
已邀请:
正确的做法是编写有效的JavaScript。在您的代码示例中,您经常忽略重要的分号。请记住,尽管javascript可以很好地容忍语法问题,但它们可能以意想不到的方式使您陷入麻烦。
test2 = function(){console.log(\"test2\")};
应该
test2 = function(){console.log(\"test2\");};
因为两个赋值都是一条语句,所以是
console.log()
方法调用。 当您发出无效的javascript时,很可能会发生各种奇怪的事情。尽管大多数情况都会导致javascript语法错误,但KNS用于包装代码(并防止意外的变量泄漏)的闭包可能会产生副作用。 简而言之:如果这里有错误,那就是javascript解析器的灵活性,可让您省略重要的分号。这不被视为KRL错误。     

要回复问题请先登录注册