Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Prototype: mastering JS object orientation with...

Prototype: mastering JS object orientation without classes

Presentation used in JSDay Recife 2015

Luiz Augusto

December 12, 2015
Tweet

More Decks by Luiz Augusto

Other Decks in Technology

Transcript

  1. class Person { constructor(props) { this.name = props.name; this.age =

    props.age; } introduce() { console.log(`Hi. My name is ${this.name}. I am ${this.age} years old.`); } getOlder() { this.age += 1; } } const p1 = new Person({ name: 'John', age: 20 }); p1.getOlder(); p1.introduce(); // My name is John. I am 21 years old.
  2. { name: 'John', age: 20 } class Person { constructor(props)

    { this.name = props.name; this.age = props.age; } introduce() { … } getOlder() { … } } const p1 = new Person({ name: 'John', age: 20 }); p1.getOlder(); p1.introduce(); // My name is John. I am 21 years old. Object.getPrototypeOf(p1); { introduce() { ... }, getOlder() { ... } }
  3. Object.getPrototypeOf({}); { hasOwnProperty() { ... }, isPrototypeOf() { ... },

    propertyIsEnumerable() { ... }, toLocaleString() { ... }, toString() { ... }, valueOf() { ... } }
  4. const p1 = new Person({ name: 'John', age: 20 });

    const p2 = new Person({ name: 'Doe', age: 40 }); const p3 = new Person({ name: 'Foo', age: 30 }); ... const p1000 = new Person({ name: 'Thousand', age: 18 }); Object.getPrototypeOf(p1); Object.getPrototypeOf(p2); Object.getPrototypeOf(p3); ... Object.getPrototypeOf(p1000); { introduce() { ... }, getOlder() { ... } }
  5. const personProto = { introduce() { console.log(`Hi. My name is

    ${this.name}. I am ${this.age} years old.`); }, getOlder() { this.age += 1; } }; const p1 = Object.create(personProto); p1.name = 'John'; p1.age = 20;
  6. const personProto = { introduce() { console.log(`Hi. My name is

    ${this.name}. I am ${this.age} years old.`); }, getOlder() { this.age += 1; } }; function person(props) { const p = Object.create(personProto); p.name = props.name; p.age = props.age; return p; } const p1 = person({ name: 'John', age: 20 });
  7. function person(state) { return { introduce() { console.log(`Hi. My name

    is ${state.name}. I am ${state.age} years old.`); }, getOlder() { state.age += 1; } } } const p1 = person({ name: 'John', age: 20 }); p1.getOlder(); p1.introduce(); // Hi. My name is John. I am 21 years old. p1.name; // undefined p1.age; // undefined
  8. class Dog { poop() {} bark() {} } class Cat

    { poop() {} meow() {} } Inheritance class Animal { poop() {} } class Dog extends Animal { bark() {} } class Cat extends Animal { meow() {} }
  9. Inheritance class CleaningRobot { drive() {} clean() {} } class

    MurderRobot { drive() {} murder() {} } class Robot { drive() {} } class CleaningRobot extends Robot { clean() {} } class MurderRobot extends Robot { murder() {} }
  10. function barker(state) { return { bark() { console.log(`Woof, I am

    ${state.name}`); } }; } function driver(state) { return { drive() { state.position += state.speed; } }; } function killer(state) { return { kill() { console.log('Just killed something'); } }; }
  11. function murderRobotDog(name) { const state = { name, speed: 100,

    position: 0 }; return Object.assign({}, barker(state), driver(state), killer(state)); } const rex = murderRobotDog('Rex'); rex.bark(); // Woof, I am Rex rex.kill(); // Just killed something rex.drive();
  12. The end function presententionEnder() { return { end() {} };

    } const the = presententionEnder(); the.end();