-2

这里有两个规则集说明了一个完全出乎意料的行为。这是一个错误吗?

即使没有在任何地方调用函数或脚本因 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');

          |>;  
    }

  }




}
4

1 回答 1

2

正确的做法是编写有效的 javascript。在您的代码示例中,您经常省略重要的分号。请记住,虽然 javascript 对语法问题相当宽容,但它们可能会以意想不到的方式给您带来麻烦。

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

应该

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

因为赋值既是语句,又是console.log()方法调用。

当您发出无效的 javascript 时,可能会发生各种奇怪的事情。虽然大多数会导致 javascript 语法错误,但 KNS 用于包装代码(并防止意外变量泄漏)的闭包可能会产生您观察到的副作用。

简而言之:如果这里有错误,那就是 javascript 解析器允许您省略重要分号的灵活性。这不被视为 KRL 错误。

于 2011-04-16T03:40:16.363 回答