[Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)

Do you get lost when working with functions and the new keyword? Prototypal inheritance can be completely replicated without either of those two concepts. In this lesson we will convert an object created from the new keyword against a function, to simply objects linked to other objects.

 

Sometime If you find yourself doing `new` too much, for example:

function House(color){
    this.color = color
}

const myHouse = new House('white')

console.log(myHouse.color)  // white

 

It is possible just using Object, instead of constructort:

const house = {
 set houseColor(color){
     this.color = color
 }
}

const myHouse = Object.create(house)

console.log(myHouse)  // {color: 'white'}

Our end result is the same. We didn't have worry about creating a function and calling it with the new keyword. This pattern is called OLOO, or objects linking to other objects.

Since prototypes are simply objects, objects can be created in a manner so that they're easily delegated as prototypes of other objects. Object.create gives us the ability to easily create new objects that have specifically delegated prototype objects.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值