js 原型的组合继承

25 篇文章 0 订阅
时光流逝,记录下原型的组合继承,
// 创建父类
function Parent(name){
	this.name = name ;//实例属性
	this.sayHi = function(){
		console.log("我是一个实例的方法");
	}
}
Parent.prototype.sayHello = function(){
	console.log("我是父类原型上一个sayHello方法,哈哈");
}

//创建子类
function Child(name,age){
	//继承父类的实例的属性和方法
	Parent.call(this,name);
	//Parent.call(this,argument);
	this.age = age
}
//继承父类上原型的实例
//Object.create()//es6创建一个对象,直接继承父类里面的属性和方法
Child.prototype = Object.create(Parent.prototype);
//实例的原型重新指向自己,不然指向的是Parent
Child.prototype.constructor = Child;
//第二种方法也可以继承
//Child.prototype.__proto__ = Parent.prototype;
let child = new Child("嘿嘿",20);
console.log(child.name); //嘿嘿
console.log(chid.age);//20
console.log(child.sayHi());//我是一个实例的方法
console.log(child.sayHello());//我是父类原型上一个sayHello方法,哈哈
console.log(child.__proto__ === Child.prototype);//true
console.log(child.constructor === Child);//true child实例的constructor指向构造函数的原型
console.log(Child.prototype.constructor === Child);//true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值