var AAA = function(name, age) { this.name = name ; this.age = age; } AAA.prototype.getName = function() { console.log(this.name); } AAA.prototype.getAge = function() { console.log(this.age); } var BBB = function(name, age, comments) { if(this instanceof BBB) { AAA.call(this, name, age); this.comments = comments; } else{ return new BBB(name, age, comments); } }; BBB.prototype = new AAA(); BBB.prototype.getComments = function() { console.log(this.comments); } var bbb = BBB(‘mhp‘, 17, ‘b‘); console.dir(bbb); bbb.getName(); bbb.getAge();// 17 bbb.getComments();// b console.log(BBB.prototype instanceof AAA);
时间: 2024-10-14 17:15:46