javascript与php观察者模式

2023-05-19,,

  1. JAVASCRIPT:
    a. 代码:
    var a = function(){
    this.int = 0
    this.add = function(int){
        this.int = int
    }
    this.watch = function(){
        if(this.int==0){
            console.log('0')
        }else{
            console.log('not 0')
        }
    }
    }
    var a = new a()
    a.watch()
    a.add(1)
    a.watch()

    b. 输出:

    0
    not 0
  2. PHP
    a. 代码:
    <?php
    class a{
    public $int = 0;
    public function add($int){
        $this->int += $int;
    }
    public function watch(){
        if($this->int==0){
            echo "0".PHP_EOL;
        }else{
            echo "not 0".PHP_EOL;
        }
    }
    }
    $a = new a();
    $a->watch();
    $a->add(1);
    $a->watch();

    b. 输出:

    0
    not 0

《javascript与php观察者模式.doc》

下载本文的Word格式文档,以方便收藏与打印。