Knowledge base
es
es
  • Base de conocimiento
  • Bash scripting
    • Echemos un vistazo
  • Jenkins
    • Echemos un vistazo
  • Kafka
    • Echemos un vistazo
  • Kubernetes / Docker
    • Echemos un vistazo
  • Node.js
    • Echemos un vistazo
    • Node binary executable
      • Evaluando dinámicamente el código
      • Precarga de módulos
      • Límite de pila de datos (Stack trace)
    • Depuración y diagnóstico
    • Enfoque práctico de javascript
      • Tipos de datos
      • Funciones
      • Herencia de prototipos
        • Funcional
        • Funciones de constructor
        • Constructor (Class-Syntax)
      • Closure scope
    • Paquetes y dependencias
      • Inicializando paquetes
      • Dependencias
      • Package scripts
    • Sistema de módulos
    • Flujo de control asíncrono
      • Callbacks
      • Promises
      • Async/await
  • OAuth 2.0 / OpenID Connect
    • Echemos un vistazo
    • OpenID Connect
    • OAuth 2.0
      • Mejores prácticas de cómo manejar tokens
  • OpenShift
    • Echemos un vistazo
  • OWASP
    • Echemos un vistazo
  • SAML 2.0
    • Echemos un vistazo
  • SSL / TLS
    • Echemos un vistazo
  • Terraform
    • Echemos un vistazo
Powered by GitBook
On this page

Was this helpful?

  1. Node.js
  2. Enfoque práctico de javascript

Herencia de prototipos

PreviousFuncionesNextFuncional

Last updated 3 years ago

Was this helpful?

Existen varios enfoques y variaciones de cadena de prototipos:

Después de revisar los temas que se describen anteriormente, comprenderemos el siguiente ejemplo:

const assert = require('assert');

class Leopard {
  constructor(name) {
    this.name = name;
  }
  hiss() {
    console.log(`${this.name}: hsss`);
  }
}

class Lynx extends Leopard {
  constructor(name) {
    super(name);
  }
  purr() {
    console.log(`${this.name}: prrr`);
  }
}

class Cat extends Lynx {
  constructor(name) {
    super(`${name} the cat`);
  }
  meow() {
    console.log(`${this.name}: meow`);
  }
}

const felix = new Cat('Felix');
felix.meow(); // Felix the cat: meow
felix.purr(); // Felix the cat: prrr
felix.hiss(); // Felix the cat: hsss

// Prototype checks...

const felixProto = Object.getPrototypeOf(felix);
const felixProtoProto = Object.getPrototypeOf(felixProto);
const felixProtoProtoProto = Object.getPrototypeOf(felixProtoProto);
assert(Object.getOwnPropertyNames(felixProto).length, 1);
assert(Object.getOwnPropertyNames(felixProtoProto).length, 1);
assert(Object.getOwnPropertyNames(felixProtoProto).length, 1);
assert(typeof felixProto.meow, 'function');
assert(typeof felixProtoProto.purr, 'function');
assert(typeof felixProtoProtoProto.hiss, 'function');
console.log('prototype checks passed!');
const assert = require('assert');

function Leopard(name) {
  this.name = name;
}

Leopard.prototype.hiss = function () {
  console.log(`${this.name}: hsss`);
};

function Lynx(name) {
  Leopard.call(this, name);
}

function inherit(proto) {
  function ChainLink() {}
  ChainLink.prototype = proto;
  return new ChainLink();
}

Lynx.prototype = inherit(Leopard.prototype);

Lynx.prototype.purr = function () {
  console.log(`${this.name}: prrr`);
};

function Cat(name) {
  Lynx.call(this, `${name} the cat`);
}

Cat.prototype = inherit(Lynx.prototype);

Cat.prototype.meow = function () {
  console.log(`${this.name}: meow`);
};

const felix = new Cat('Felix');
felix.meow(); // Felix the cat: meow
felix.purr(); // Felix the cat: prrr
felix.hiss(); // Felix the cat: hsss

// Prototype checks...

const felixProto = Object.getPrototypeOf(felix);
const felixProtoProto = Object.getPrototypeOf(felixProto);
const felixProtoProtoProto = Object.getPrototypeOf(felixProtoProto);
assert(Object.getOwnPropertyNames(felixProto).length, 1);
assert(Object.getOwnPropertyNames(felixProtoProto).length, 1);
assert(Object.getOwnPropertyNames(felixProtoProto).length, 1);
assert(typeof felixProto.meow, 'function');
assert(typeof felixProtoProto.purr, 'function');
assert(typeof felixProtoProtoProto.hiss, 'function');
console.log('prototype checks passed!');
const assert = require('assert');

const leopard = {
  hiss: function () {
    console.log(`${this.name}: hsss`);
  }
};

const lynx = Object.create(leopard, {
  purr: {
    value: function () {
      console.log(`${this.name}: prrr`);
    }
  }
});

const cat = Object.create(lynx, {
  meow: {
    value: function () {
      console.log(`${this.name}: meow`);
    }
  }
});

const felix = Object.create(cat, {
  name: {
    value: 'Felix the cat'
  }
});

felix.meow(); // Felix the cat: meow
felix.purr(); // Felix the cat: prrr
felix.hiss(); // Felix the cat: hsss

// Prototype checks...

const felixProto = Object.getPrototypeOf(felix);
const felixProtoProto = Object.getPrototypeOf(felixProto);
const felixProtoProtoProto = Object.getPrototypeOf(felixProtoProto);
assert(Object.getOwnPropertyNames(felixProto).length, 1);
assert(Object.getOwnPropertyNames(felixProtoProto).length, 1);
assert(Object.getOwnPropertyNames(felixProtoProto).length, 1);
assert(typeof felixProto.meow, 'function');
assert(typeof felixProtoProto.purr, 'function');
assert(typeof felixProtoProtoProto.hiss, 'function');
console.log('prototype checks passed!');

Funcional
Funciones de constructor
Constructor (Class-Syntax)