博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript] Replicate JavaScript Constructor Inheritance with Simple Objects (OLOO)
阅读量:7207 次
发布时间:2019-06-29

本文共 1100 字,大约阅读时间需要 3 分钟。

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.

转载地址:http://bprum.baihongyu.com/

你可能感兴趣的文章
四、Linux/UNIX操作命令积累【chmod、chown、tail】
查看>>
使用相机闪光灯开启
查看>>
机器学习中的数学(3)-模型组合(Model Combining)之Boosting与Gradient Boosting
查看>>
Java实现文件的加密与解密
查看>>
在开发过程中调试报表插件详细教程
查看>>
Android瀑布流照片墙实现,体验不规则排列的美感
查看>>
重点关注之OData with List
查看>>
Action的动态调用方法
查看>>
[CareerCup] 5.3 Next Binary Representation 下一个二进制表达
查看>>
jQuery整理笔记2----jQuery选择整理
查看>>
C语言中使用结构体
查看>>
Memcache功能具体解释
查看>>
ADB shell出现error:device offline提示
查看>>
hdu 1575 Tr A(矩阵高速电源输入)
查看>>
数据结构--图 的JAVA实现(下)
查看>>
免费开源3D模型设计软件汇总
查看>>
模板 lucas
查看>>
uboot中CMD的实现
查看>>
ipconfig命令
查看>>
模板最近的共同祖先
查看>>