Oops - Javascript

  • Prototype
    • JS Objects have a special property called prototype that is either null or references another object
    • Prototypical Inheritance
      • When reading a property tha tis missing, taken from Prototype
    • Syntax
          let variable = {
              functionName1: () => {}
          }
          variable.__proto__ = {
              functionName2: () => {}
          }
          variable.functionName2();
      
  • Class
        class ClassName {
            functionName() {}
            constructor() {}
        }
        let variable = new ClassName();
    
  • Object
        let variable = new ClassName();
    
  • Inheritance
        class ClassName1 {}
        class ClassName2 extends ClassName1 {
            // Call constructors and functions of parent class
            super(arguments);
            super.functionName(arguments);
        }
    
  • Overriding Constructor
        class ClassName2 extends ClassName1 {
            // Created automatically
            constructor(...arguments) {
                super(...arguments);
            }
            // Custom
            constructor() {
                super();
                // this statements must be written after super()
            }
        }
    
  • Static Methods
        class ClassName {
            static functionName() {
                return value;
            }
        }
        // Calling
        ClassName.functionName();
    
  • Getter, Setter
        class ClassName {
            get variable() {
                return this._variable;
            }
            set variable(newVariable) {
                this._variable = newVariable;
            }
        }
    
  • Others
    • variable instanceof className => Returns boolean value true if object is a instance of class or any other class inheriting from it
Share: