123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              // 维护一系列的Observer, 可以对它们进行添加、删除和通知的操作.
class Subject {
    constructor() {
        // 这里是为参与者腾出的空间.
        this.observerList = [];
    }
    addObserver(observer) {
        // 招待每一个参与者, 就是引导参与者进入腾出的空间.
        this.observerList.push(observer);
    }
    removeObserver(observer) {
        // 对于不再感兴趣的Observer,可以将其剔除,其不会再收到任何来自Subject的通知.
        let pos = this.observerList.indexOf(observer);
        ~pos && this.observerList.splice(pos, 1);
    }
    notify(order) {
        // context 是Subject传达的指令
        // 由公司提供的统一对接方式,向在场的每一个Observer传达并让其执行.
        this.observerList.forEach(observer => observer.update(order));
    }
}

// 是为Subject状态发生变化需要获得通知的对象提供更新接口.
class Observer {
    constructor(fn) {
        // update是公司的对接方式, fn是员工的职能.
        this.update = fn
    }
}

// 公司派遣员工.(观察者们)
const observer1 = new Observer((order) => {
    document.write(`${order}: 我是财务, 我要开始算绩效发工资了<br/>`)
    });
const observer2 = new Observer((order) => {
    document.write(`${order}: 我是后勤客服, 有什么事情需要帮助的?<br/>`)
    });
const observer3 = new Observer((order) => {
    document.write(`${order}: 我是人事, 我要招人了<br/>`)
    });

// 事务繁忙, 派了一个影分身.(Subject添加Observer)
const subject = new Subject();
// 引导observer1进入observer1房间.
subject.addObserver(observer1);
// 引导observer2进入observer1房间.
subject.addObserver(observer2);
// 引导observer3进入observer1房间.
subject.addObserver(observer3);

// 将不再感兴趣的员工剔除队伍,请出房间.(Subject删除Observer)
subject.removeObserver(observer2);

// 下达指令.(Subject通知Observer)
subject.notify('描述下各自的职能');
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console