javascript与php单例模式
var test = function (){this.str = '',this.a = function (){ this.str += "a" return this},this.b = function(){ this.str += "b" return this}this.out = function(){ console.log(this.str)}}var entity = new test()entity.a().b().out()
输出:
ab
二、PHP:代码:
<?phpclass single{public $out;public function a($a){ $this->out .= $a; return $this;}public function b($b){ $this->out .= $b; return $this;}public function say(){ echo $this->out.PHP_EOL;}public function get(){ return $this->out;}}$single = new single();$out = $single->a('a')->b('b')->say();$get = $single->a('a')->b('b')->get();echo $get;
输出:
ababab
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。