继承可以通过原型链继承、借用继承、组合继承,由于原型链继承和借用继承存在一定的缺陷。那么本文着重讲组合继承,又由于本人不喜欢讲概念,所以下面直接上代码:
<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
function Parent(age){
this.age=age;
}
Parent.prototype.showAge=function(){
console.log(this.name+""+this.age);
}
function Son(name,age){
this.name=name;
Parent.call(this,age)
}
Son.prototype=new Parent();
var son=new Son("你的名字:","新海诚");
son.showAge();
</script>
</body>
</html>