[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.
相关文章
- JS读书心得:《JavaScript框架设计》——第12章 异步处理
- javascript点击变绿色再点击变红色
- 第一百节,JavaScript表达式中的运算符
- javascript遍历控件(实例详解)
- JavaScript 库-localforage离线存储
- [Javascript] Create an Image with JavaScript Using Fetch and URL.createObjectURL
- [Javascript] Wrap an API with a Proxy
- [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript
- [Javascript] Classify JSON text data with machine learning in Natural
- [Javascript ] Array methods in depth - sort
- [Javascript + rxjs] Simple drag and drop with Observables
- [Javascript] Get Started with LeafletJS Mapping
- JavaScript中判断对象类型的种种方法
- JavaScript文件操作(1)-基础
- javascript with关键字简单用法
- [Functional Programming] Create Reusable Functions with Partial Application in JavaScript
- [Javascript Crocks] Recover from a Nothing with the `coalesce` Method
- [Javascript Crocks] Safely Access Object Properties with `prop`
- [Javascript] Iterate Over Items with JavaScript's for-of Loop
- [Javascript] Validate Data with the Every() Method
- [Javascript + rxjs] Using the map method with Observable
- Maven build中隐藏的SAP UI5 JavaScript merge任务
- JavaScript, ABAP和Scala里的尾递归(Tail Recursion)
- JavaScript里的...(三个点)操作符
- Javascript创建对象的学习和使用
- JavaScript 数据类型
- JavaScript 前端笔记总结(精简)