function Waiter(id, name, salary) { // 创建了一个Waiter类
        Employees.call(this, id, name, salary) // 这里Waiter继承了Employees,Employees是个父类,也可以没有
}
    Waiter.prototype = Object.create(Employees.prototype);
    Waiter.prototype.constructor= Waiter;
    Waiter.prototype.work = function (arg) { // 重写原型上的方法
        if (arg instanceof Array){ //数组的话,记录点菜
            console.log(‘finish order dish $记录work‘);
            return this;
        } else { //上菜行为
            console.log(‘finish serving a dish $记录work‘)
        }
    };
    // cook调用的方法,返回菜单
    Waiter.prototype.tellCookTheMenu = function () {
        return this.menu;
    };
    // cook调用的方法,拿到做好的菜
    Waiter.prototype.serving = function () {
        this.work();// 上菜行为
        this.customer.eat();
    };
 return {
        name: ‘waiter‘,
        getWaiterInstance: function (...arg) {
            if (!waiter) {
                waiter = new Waiter(...arg)
            }
            return waiter;
        }
    }
//服务员 单例
var waiterSingle = (function () { // 是一个立即执行函数,并将执行的结果赋值给waiterSingle
    var waiter = null; // 实例存在这个变量里
    function Waiter(id, name, salary) {
        Employees.call(this, id, name, salary)
    }
    Waiter.prototype = Object.create(Employees.prototype);
    Waiter.prototype.constructor= Waiter;
    Waiter.prototype.work = function (arg) { // 重写原型上的方法
        if (arg instanceof Array){ //数组的话,记录点菜
            console.log(‘finish order dish $记录work‘);
            return this;
        } else { //上菜行为
            console.log(‘finish serving a dish $记录work‘)
        }
    };
    // cook调用的方法,返回菜单
    Waiter.prototype.tellCookTheMenu = function () {
        return this.menu;
    };
    // cook调用的方法,拿到做好的菜
    Waiter.prototype.serving = function () {
        this.work();// 上菜行为
        this.customer.eat();
    };
    // 从顾客order方法,拿到点的菜
    Waiter.prototype.getMenu = function (arg) {
        this.customer = arg;
        this.menu = arg.dish;
        console.log(‘waiter get the menu‘, this.menu);
        return this;
    };
    return {
        name: ‘waiter‘,
        getWaiterInstance: function (...arg) {
            if (!waiter) {  // 判断如果waiter里没有,则new,并赋值给waiter
                waiter = new Waiter(...arg)
            }
            return waiter;
        }
    }
})();
var waiter = waiterSingle.getWaiterInstance(2, ‘Lucy‘, 5000);
原文:https://www.cnblogs.com/yadiblogs/p/9433836.html