javascript类式继承模式#1——默认模式

时间:2023-03-09 15:10:50
javascript类式继承模式#1——默认模式
 <!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>类式继承模式#1——默认模式</title>
</head> <body>
<script type="text/javascript"> function Parent(name){
this.name=name||'Adam';
}; Parent.prototype.say=function(){
return this.name;
}; function Child(name){}; inherit(Child,Parent); function inherit(C,P){
C.prototype=new P();
} /***************************************/ var kid=new Child('Janking'); console.log(kid.say()) //缺点:inherit()并不支持将参数传递到子构造函数中,而子构造函数然后又将参数传递到父构造函数中。 </script>
</body>
</html>