这里有两个规则集说明了一个完全出乎意料的行为。这是一个错误吗?
即使没有在任何地方调用函数或脚本因 javascript 错误而中断,函数也会被执行。解决方案是始终以分号结束声明,如下所示:
test1 = function(){console.log("test1");} ;// <==
但我认为这很危险,因为这在 javascript 中不是强制性的。
示例 1 小书签:
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);})()
示例 2 小书签:
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);})()
规则集:
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');
|>;
}
}
}