javascript与php注册自己的“事件”
- JAVASCRIPT:
a. 代码:var a = function(){ this.eve this.func this.on = function(eve,func){ this.eve = eve this.func = func } this.emit = function(eve){ if(this.eve == eve){ var func = this.func func() } } } var aa = new a() aa.on('click',function(){ console.log('im click') }) aa.emit('click')
b. 输出:
im click
- PHP:
a. 代码:<?php class a{ public $eve; public $func; public function on($eve,$func){ $this->eve = $eve; $this->func = $func; } public function emit($eve){ if($this.eve == $eve){ $func = $this->func; $func(); } } } $a = new a(); $a->on('click',function(){ echo "im click"; }); $a->emit('click');
b. 输出:
im click