Javascript | Javascript kata |Node JS | Data Types | Variables | Control Flow | Scope and Environment | Objects | Functions | Events | Prototypal Inheritance | Three.js | 3DE Code Editor |

Prototypal Inheritance

  • hidden property [[Prototype]] - null or a reference to other object
  • 
    prototype obj
         ^
         | [[Prototype]]
         |
        obj
    
    
  • When we read a property from object, and it’s missing, JavaScript takes it from the prototype
  • obj.__proto__ can be used to set it
    let earphones = { speakers: 2 };
    
    let jbl       = { bluetooth: true };
    
    jbl.__proto__ = earphones;  // sets jbl.[[Prototype]] to earphones
    
    alternatively:
    let jbl       = {
      bluetooth: true,
      __proto__: earphones
    };
    
  • Sources, External Links

  • javascript.info/prototype-inheritance
  • http:///wiki/?jsprototypalinheritance

    12apr23   admin