<!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>类式继承模式#4——共享原型</title>
</head>
<body>
<!--
共享原型继承:可复用成员应该转移到原型中而不是放置在this中。因此,处于继承的目的,任何值得继承的东西都应该放置在原型中实现。所以,可以仅将子对象的原型与父对象的原型设置为相同即可。这种模式能够简短而迅速的原型链查询。
缺点:如果在继承链下方的某处存在一个子对象或孙子对象修改了原型,它将会影响到所有的父对象和祖先对象。
-->
<script type="text/javascript">
function Parent(name){};
Parent.prototype.say=function(){
console.log(this.name);
};
function Child(name){};
function inherit(C,P){
C.prototype=P.prototype;
}
inherit(Child,Parent)
/************************************/
var parent=new Parent();
var child=new Child();
console.log(child)
</script>
</body>
</html>