js创建对象的构造函数模式+原型模式和组合继承Hybrid Pattern & combination inheritance

    xiaoxiao2021-03-25  122

    HybridPatternExample

    function Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.friends = ['A', 'B']; } Person.prototype = { constructor : Person, sayName : function () { console.log(this.name); } } function SuperType(name) { this.name = name; this.color = ['red', 'blue']; } SuperType.prototype.sayName = function () { console.log(this.name); } function SubType(name, age) { //继承属性 SuperType.call(this, name); this.age = age; } //继承方法 SubType.prototype = new SuperType(); SubType.prototype.constructor = SubType; SubType.prototype.sayAge = function () { console.log(this.age); }

    转载请注明原文地址: https://ju.6miu.com/read-8295.html

    最新回复(0)