[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.
相关文章
- javascript 基础_JavaScript高级编程
- javascript 使用btoa和atob来进行Base64转码和解码
- 【说站】javascript函数节流是什么
- 【说站】JavaScript全局作用域是什么
- ORA-39322: Cannot use transportable tablespace with TIMESTAMP WITH TIME ZONE columns and different time zone version. ORACLE 报错 故障修复 远程处理
- JavaScript学习总结(五)——Javascript中==和===的区别详解编程语言
- Oracle 中的 WITH 语句使用技巧(oracle with用法)
- 分析学习Oracle深入理解使用With 分析(oracle使用with)
- MySQL查询优化使用WITH子句的限制与替代方案(mysql不能用with)
- 从Oracle中挖掘洞见坚实的With表(oracle with表)
- javascript实现的一个图片转移效果
- javascript下with的简化代码写法
- JavaScript基本入门语法集合
- JavaScript验证浏览器是否支持javascript的方法小结
- javascript表单中浏览文件的“浏览”按钮修改
- Javascript面向对象命名空间
- 关于JavaScript的with语句的使用方法
- JavaScript高级程序设计阅读笔记(十六)javascript检测浏览器和操作系统-detect.js
- javascript中常用编程知识
- 讨论html与javascript在浏览器中的加载顺序问题
- javascript生成json数据简单示例分享
- javascript面向对象之this关键词用法分析
- javascript中with()方法的语法格式及使用