Herencia en base a prototipos # La herencia es una forma de reutilización del código, mediante la cual se puede extender un molde, sea este una clase o un prototipo. # Los prototipos son objetos comúnes y corrientes que son utilizados como base para crear nuevas instancias, es decir, en JavaScript históricamente no existen las clases, sólo instancias. Los objetos heredan de otros objetos. # En JavaScript existe la orientación a objetos basada en clases actualmente, sin embargo esta es sólo azúcar sintáctica que internamente hace uso de prototipos. # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model#summary_of_differences # # const person6 = new person5.constructor('Edward Snowden, 37, ['privacidad', 'transparencia']); Formas de crear un objeto - object literal: {} # const person1 = {name: 'Felix', age: 30, interests: ['libertad del conocimiento', 'descentralización del poder']} - class: class ClassName { constructor(...) { ... } } + new # class Person { # constructor(name, age, interests) { # this.name = name; # this.age = age; # this.interests = interests; # } # # greeting() { # console.log(`¡Hola! Mi nombre es ${this.name}`); # } # } - constructor function: function FunctionConstructor + new # A "constructor" in JavaScript is "just" a function that happens to be called with the new operator. # function Person(name, age, interests) { # this.name = name; # this.age = age; # this.interests = interests; # } # Person.prototype.greeting = function() { # console.log(`¡Hola! Mi nombre es ${this.name}`); # } Object.prototype.constructor # Todos los objetos instanciados con `new` tienen una propiedad en su prototipo que alberga el objeto con que fueron instanciados. # const person6 = new person5.constructor('Edward Snowden, 37, ['privacidad', 'transparencia']); # obj.prototype != Object.getPrototypeOf(obj) Object.create # var o = { # a: 2, # m: function() { # return this.a + 1; # } # }; # # console.log(o.m()); // 3 # // When calling o.m in this case, 'this' refers to o # # var p = Object.create(o); # // p is an object that inherits from o # # p.a = 4; // creates a property 'a' on p # console.log(p.m()); // 5 # obj.isPrototypeOf(proto) # La modificación de un prototipo afecta a todas sus instancias Formas de extender la cadena de prototipos - obj.prototype - Object.create(prototype) # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create - Object.setPrototypeOf(obj, prototype) # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf Formas de heredar - class Child extends Parent{ constructor(...) { super(...); ... } # class Hacktivista extends Person { # constructor(name, age, interests, activity) { # super(name, age, interests); # this.activity = activity; # } # doIt() { # console.log(this.activity + '!'); # } # } - function Child { Parent.call(this, ...); } + Child.prototype = Object.create(Parent.prototype) + Object.defineProperty(Child.prototype, 'constructor', ... } # function Hacktivista(name, age, interests, activity) { # Person.call(this, name, age, interests); # this.activity = activity; # } # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call # Hacktivista.prototype = Object.create(Person.prototype); # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create # Object.defineProperty(Teacher.prototype, 'constructor', { # value: Hacktivista, # enumerable: false, // so that it does not appear in 'for in' loop # writable: true # }); # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties # Hacktivista.prototype.doIt = function() { # console.log(this.activity + '!'); # } # https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object_prototypes # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Details_of_the_Object_Model # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain Miembros de un objeto - propiedades de una instancia # this.algo - propiedades de un prototipo # constructor.prototype - propiedades privadas # sin this - propiedades estáticas # ConstructorFunction.staticMethod || static