继承方式

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>
/* JSON.stringify(Json名字)获取JSon的内容
继承:子类从父类获取父类的属性和方法,且子类添加属性或方法,不影响父类。
第一种:构造函数继承!!!
第二种:克隆对象继承!!!
第三种:类式继承 Pchildren.prototype= new Parent()
第四种:组合式继承(构造+类式)
第五种:组合式寄生继承(构造+寄生)!!!!!借助新建实例
function Fn(){};Fn.prototype=Parent.prototype;Pchildren.prototype=new Fn();

*/
function Parent(name,age){
this.name=name;
this.age=age
}
Parent.prototype.showName=function(){
console.log(this.name);
};
//构造函数继承
function Pchildren(name,age,address){
Parent.call(this,name,age);
this.address=address
}
// Pchildren.prototype=Parent.prototype;//引用类型,改变指针两个原型同时指向一个内存,任意改变一个都会改变对象原型,
// 不符合继承的条件
Pchildren.prototype= new Clone(Parent.prototype);
Pchildren.prototype.showAddress=function(){
console.log(this.address)
};
var f2= new Parent("parent",30);
var f1= new Pchildren("children",19,"射洪");
f1.showName();
f1.showAddress();
/*Pchildren.prototype=Clone(Parent.prototype);
//对象的clone 一
function Clone(obj){
var newObj={};
for(var key in obj){
if(typeof obj[key]!="object"){
newObj[key]=obj[key];
}else{
newObj[key]=Clone(obj[key]);
}
}
return newObj;
}*/
// 对象的克隆 二
function Clone(obj){
for(var key in obj)
this[key]=(typeof obj[key]!="object")?obj[key]:new Clone(obj[key])
}
</script>
</body>
</html>
posted @ 2018-03-09 11:12  Tutao1995  阅读(166)  评论(0编辑  收藏  举报