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();
单例模式
最新推荐文章于 2024-07-20 23:54:17 发布