在规则集全局部分的emit中,函数声明后面没有分号



这里有两个规则集,说明了一个完全意想不到的行为。这是臭虫吗?

函数被执行,即使它们没有在任何地方调用或脚本因javascript错误而中断。解决方案是总是用分号结束声明,像这样:

test1 = function(){console.log("test1");}  ;// <==

但是我认为这很危险因为这在javascript中不是强制性的

示例1 bookmarklet:

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 bookmarklet:

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解析器允许您省略重要分号的灵活性。

相关内容

  • 没有找到相关文章