第一种方法 构造函数继承
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//第一种:构造函数的继承
function Cat(name,color){
this.name = name;
this.color = color;
this.trait = function(){
console.log('搞笑');
}
}
Cat.prototype.skill = function(){ //原型上的属性方法
console.log('捕鱼');
}
//需求:狗要卖萌,狗要会捕鱼
function Dog(name,color,food){
this.food = food;
Cat.call(this,name,color);
}
var dog1 = new Dog('小花','gray','屎');
//测试方法
console.log(dog1.name);
console.log(dog1.food);
dog1.trait();
dog1.skill(); //no found
//局限性:“父类”原型上的属性方法无法继承,所以二哈没有捕鱼的技能
</script>
</body>
</html>