单例模式

来源:互联网 发布:优化发展环境讲话 编辑:程序博客网 时间:2024/06/06 08:50
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();
原创粉丝点击