原型链继承相关prototype

原型链-由相互关联的原型组成的链状结构;其中有三种部分组成:构造函数 实例 原型对象。
特点:

  1. 构造函数才有prototype;实例才有__proto__
  2. 构造函数是Function的实例,且构造函数的prototype是Object的一个实例
  3. Function.prototype等于Function.proto
  4. Object.prototype.__proto__等于null

模拟new
function Animal() {
this.name = ‘mike’;
}
Animal.prototype.selfFun = function () {
console.log(this.name)
}
function fun1 (fn) {
const obj = {}
obj.proto = fn.prototype
fn.apply(obj, [])
return obj
}
const testFun = new fun1(Animal)
testFun.selfFun()

模拟继承
function Animal() {
this.name = ‘mike’;
}
Animal.prototype.selfFun = function () {
console.log(this.name)
}
function Fun1() {}
Fun1.prototype = new Animal()
const test = new Fun1()
test.selfFun ()
如有错误,欢迎指出~~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值