<script> /* *寄生式继承 继承原型 *传递参数 subClass 子类 *传递参数 superClass 父类 */ //宿主 function inheritObject(o){ function fn(){} fn.prototype = o; return new fn } //继承转换原型和构造函数 function inheritPrototype(subClass,superClass){ var p = inheritObject(superClass.prototype); p.constructor = subClass; subClass.prototype = p; } //父级构造函数 function superClass(name){ this.name = name; this.colors = ['red','blue','green']; } //父级的原型 superClass.prototype.getName = function(){ console.log(this.name); } //子级构造函数 function subClass(name,time){ superClass.call(this,name); this.time = time; } //子级继承父级的原型 //先函数转换,然后进行实例化 //或者先实例化,然后用__proto__属性进行转换(__proto__不建议使用) inheritPrototype(subClass,superClass); //给子级原型添加功能 subClass.prototype.getTime = function(){ console.log(this.time); } var b1 = new subClass('car',2018); console.log( b1 ); b1.getName(); </script>