码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象-原型的继承

时间:2018-09-08 11:48:34      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:解决   console   ons   pre   argument   struct   parse   原型   子类   

//父构造函数
function Parsent(name.age){
    this.name = name;
    this.age = ahe;
}
Parsent.prototype.say = function(){
	console.log("大家好,我是"+this.name);
};
function Child(name,age,job){
	Parsent.apply(this,arguments);
	this.job = job;
}
//这种方法不可取,因为这是传址,子父类会互相影响
// Child.prototype = Parsent.prototype;
				
//下面的方法可以解决继承过程中子类与父类方法会互相影响的问题
function Temp(){}   //新建一个空的构造函数
Temp.prototype = Parsent.prototype;
Child.prototype = new Temp();
Child.prototype.constructor = Child;
Child.prototype.eat = function(){
	console.log("我要吃饭")
}
var child = new Child("zs",13,"student");
console.log(child.name,child.age,child.job);
Child.prototype.say = function(){
	console.log("我是child");
}
child.say();
child.eat();
var parsent = new Parsent("kt",20);
parsent.say();

  

面向对象-原型的继承

标签:解决   console   ons   pre   argument   struct   parse   原型   子类   

原文地址:https://www.cnblogs.com/www-dw-com/p/9608258.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!