zoukankan      html  css  js  c++  java
  • [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.

  • 相关阅读:
    Live Writer配置
    protobufnet 学习手记
    好的Sql语句也能提高效率(二)
    关于CodeSmith的输出问题
    [Scrum]12.29
    [scrum] 1.4
    分享 关于c#注释的规范
    [Scrum] 1.3
    分享:将XML(VS提取注释时生成)转换为Chm的一个方法
    【Scrum】2010.12.27
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9830543.html
Copyright © 2011-2022 走看看