当规则集全局部分的发射中函数声明后面没有分号时出现错误?

发布于 2024-11-01 06:37:58 字数 2939 浏览 4 评论 0原文

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

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

          |>;  
    }

  }




}

Here are two rulesets that illustrate a totally unexpected behavior. Is this a bug?

Functions get executed even though they are not called anywhere or script breaks with a javascript error. The solution is to always end a declaration with a semicolon like this:

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

but this is dangerous I think because this is not mandatory in javascript.

Example 1 bookmarklet:

javascript:(function(){
  var d=document;var s=d.createElement('script');s.text="
  KOBJ_config={'rids':['a1135x27']};";d.body.appendChild(s);
  var l=d.createElement('script');l.src='http://init.kobj.net/js/shared/kobj-static.js';
  d.body.appendChild(l);})()

Example 2 bookmarklet :

javascript:(function(){
  var d=document;var s=d.createElement('script');s.text="
  KOBJ_config={'rids':['a1135x27']};";d.body.appendChild(s);
  var l=d.createElement('script');l.src='http://init.kobj.net/js/shared/kobj-static.js';
  d.body.appendChild(l);})()

Rulesets:

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');

          |>;  
    }

  }




}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

皇甫轩 2024-11-08 06:37:58

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

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

应该是

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

因为两者的赋值都是一个语句,都是console.log()方法的调用。

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

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

The right thing to do here is write valid javascript. In your code example, you regularly leave off important semicolons. Remember that while javascript is fairly tolerant of syntactic problems, they can get you into trouble in unexpected ways.

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

should be

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

because both the assignment is a statement, is is the console.log() method call.

When you emit invalid javascript, all sorts of odd stuff is likely to occur. While most result in a javascript syntax error, the closures that KNS uses to wrap your code (and prevent unintended variable leakage) can have the side effects you observe.

In short: If there is a bug here, it's the javascript parser's flexibility in letting you omit important semicolons. This is not considered a KRL bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文