-2

まったく予期しない動作を示す 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 に答える