单例模式

function A() {
    // 实例对象
    var instance;
    // 新的构造函数
    B = function () {
        // 返回这个实例对象
        return instance;
    };
    // 原型链继承
    B.prototype = this; // this是一个A的实例对象
    B.prototype.constructor = B;
    instance = new B();

    // A的私有属性
    var a = 1;
    // 特权函数
    instance.log = function () {
        console.log(a);
    };
    // 此处相当于经典继承
    instance.name = "name";
    return instance;
}

let a = new A();
let b = new B();
console.log(b === a);
a.log();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章