<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>继承3 混合模式()</title>
</head>
<body>
创建类最好的方法使用构造函数定义属性,用原型定义方法,这种方法同样-适用于继承机制,用对象冒充继承机制构造函数的属性,用原型链继承prototype对象的方法
<script type="text/javascript">
function Monkey(_type,_home){
this.type=_type;
this.home=_home;
}
Monkey.prototype.say=function(){
alert("我是一只快乐的猴子,家住"+this.home);
}
// 用对象冒充继承Monkey类的type属性
function Magicmonkey(_type,_home,_skill){
Monkey.call(this,_type,_home);
this.skill=_skill;
}
//用原型链继承Monkey方法
Magicmonkey.prototype=new Monkey();
Magicmonkey.prototype.run=function(){
alert("我会的技能"+this.skill)
}
var wukong = new Magicmonkey("猴子","花果山",['七十二变','筋斗云'])
alert(wukong.type);
alert(wukong.home);
wukong.say();
wukong.run();
</script>
</body>
</html>