1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| function inheritObject(o) { function F() {} F.prototype = o return new F() }
function inheritPrototype(subClass, superClass) { var p = inheritObject(superClass.prototype) p.constuctor = subClass subClass.prototype = p }
function SuperClass(name) { this.name = name this.nums = [1, 2, 3] } SuperClass.prototype.getName = function () { console.log(this.name) } function SubClass(name, time) { SuperClass.call(this, name) this.time = time } inheritPrototype(SubClass, SuperClass) SubClass.prototype.getTime = function () { console.log(this.time) }
var instance1 = new SubClass("js book", 202) instance1.nums.push(30) var instance2 = new SubClass("css book", 222)
console.log(instance1.nums) console.log(instance2.nums)
|