JavaScript es6设计模式 -Proxy(代理模式)

125次阅读
没有评论

Proxy(代理模式)

为其他对象提供一个代理以控制对这个对象的访问。

'use strict';

class Subject {
    constructor() {
        console.log('Subject Class created');
    }

    request() {
        console.log('Subject.request invoked');
    }
}

class RealSubject extends Subject {
    constructor() {
        super()
        console.log('RealSubject Class created');
    }

    request() {
        console.log('RealSubject.request invoked');
    }
}

class Proxy extends Subject {
    constructor() {
        super()
        console.log('Proxy Class created');
    }

    request() {
        this.realSubject = new RealSubject();
        this.realSubject.request();
    }
}

var proxy = new Proxy()
proxy.request()