<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>10-基于原型实现类的继承</title>
</head>
<script>
function Animal(name){
this.name=name
};
Animal.prototype={
printName:function(){
console.info("name: "+this.name);
},
eat:function(){
console.info("Animal eat");
}
}
function Lion(name){
this.name=name;
this.eat=function(){//重写父类的方法
console.info("Lion eat");
};
}
Lion.prototype=new Animal();//实现继承的核心代码,new 之后,Animal的原型不会改变,lion的原型与Animal的原型联系起来了
var lion=new Lion("One line");
lion.eat();//JavaScript会优先查找自身的方法成员,然后查找prototype中的方法以及成员
lion.printName();
</script>
<body>
</body>
</html>
javascript基础之十(基于原型实现类的继承)
最新推荐文章于 2020-10-05 20:17:27 发布