You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mechanism that allows you to eliminate redundant code.
constHTMLElement={hidden: false,innerHTML: '<div></div>',click: fnClick,focus: fnFocus,};constTextBox=Object.assing(HTMLELement,{/* new props and overrides */});constSelect=Object.assing(HTMLELement,{/* new props and overrides */});constCheckBox=Object.assing(HTMLELement,{/* new props and overrides */});
Polymorphism (Polimorfismo)
Semantic: Poly means "Many" and morphism is "Form"
Polymorphism allows us to avoid if/else and switch case statements, for example,
all these objects above should have the ability to be rendered on a page, but the way each element is rendered
is different from the others, in procedural way we have to use complex statements to do that.
Object Literals use key/values pars, where keys can be properties or methods
/*HINT* use let if you will reassign variables* use const if the variable is a constant*/constcircle={radius: 1,location: {x: 1,y: 1},draw: function(){console.log('draw');}};// Use dot notation to access properties/methodscircle.draw();
Constructor function use first letter should be uppercase as Class pattern, the Constructor too use
this keyword to refers to object that is executing this piece of code, and use new keyword to instantiante
a new Object.
// for enumateing properties from objects use "for in loop"constcircle=newCircle(10);for(letkeyincircle){// show just propertiesif(typeofcircle[key]!=='function'){console.log(key,circle[key]);// radius, 10}}// get just the keys as an arrayconstkeys=Object.keys(circle);console.log(keys);// [radius, draw]// verify if property exists into the objectif('radius'incircle){console.log('Circle has a radius.');}
// Base / Super / ParentfunctionShape(){this.computeOptimumLocation=function(){};}// The Relation "IS-A", for example: Circle is a Shape// Derived / Sub / ChildfunctionCircle(){}functionSquare(){}
Prototypes and Prototypical Inheritance
letx={};lety={};// x -> prototype (Object) -> .toString();x.toString();// [object Object](Object.getPrototypeOf(x)===Object.getPrototypeOf(y));// true
Multi-level Inheritance
Objects created by a given contructor will have the same prototype
letperson={name: 'Daniel'};letobjectBase=Object.getPrototypeOf(person);letpropertyDescriptor=Object.getOwnPropertyDescriptor(objectBase,'toString');console.log(propertyDescriptor);/* Display console.log{ configurable: true, // can delete ou not enumerable: false, // can loop through ou not value: f toString(), // the property/method value writable: true // can be overrided ou not}*/
functionCircle(radius){this.radius=radius;}Circle.prototype.draw=function(){console.log('draw');}constcircle=newCircle(2)Object.keys(circle);// returns instance members ['radius']for(letkeyincircle)console.log(key);// returns instance + prototype members -> 'radius', 'draw'circle.hasOwnProperty('radius');// true -> because radius it's a instance membercircle.hasOwnProperty('draw');// false -> because radius isn't a instance member