//设计一个拥有钩子功能的模块
var module1 = Std.module({
public:{
put:function(number){
//触发put钩子
if(!this.hooker.act("put",number)){
return;
}
Std.ajax.post("/action.php",{
number:number
});
},
print:function(text){
//触发print钩子
if(!this.hooker.act("print",text)){
return;
}
console.log(text);
}
},
main:function(){
this.hooker = new Std.hooker();
}
});
var instance1 = new module1();
//添加put钩子,如果金额小于500,put函数将不会往下执行
instance1.hooker.hook("put",function(number){
if(number < 500){
return false;
}
});
//添加text钩子,如果输出的文本内容带有 "fuck",便停止执行,并且输出 "this message has been blocked!"
instance1.hooker.hook("print",function(text){
if(text.has("fuck")){
console.log("this message has been blocked!")
return false;
}
});
//执行put方法,金额大于500,向/action.php发送了提交请求
instance1.put(600);
//执行put方法,金额小于500,那么什么也不做
instance1.put(499);
//执行print方法,输出 "hello,how are you"
instance1.print("hello,how are you");
//执行print方法,带有 "fuck" 关键字,输出 "this message has been blocked!"
instance1.print("fuck your fat ass");