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

Back to Basics: Discussing Prototypes with the Terminator

Christoph
November 18, 2016

Back to Basics: Discussing Prototypes with the Terminator

Skynet, er, frameworks have taken over the world. With the help of the Terminator you will learn how to stabilise humanity again. Or at least to stabilise your understanding of object-oriented JavaScript.

Learn the difference between class-based OO languages and prototypal languages. Understand why it is worth learning the prototypal way of doing OO and how it can help you write cleaner and simpler code.

Christoph

November 18, 2016
Tweet

More Decks by Christoph

Other Decks in Programming

Transcript

  1. November 18, 2016 Back to Basics: Discussing Prototypes with the

    Terminator Christoph Gockel @ChristophGockel
  2. “I wanna ask you a bunch of questions. And I

    want to have them answered immediately.”
  3. WAT

  4. ES6

  5. Classes in ES6 class Person { constructor(name) { this.name =

    name; } sayName() { console.log(this.name); } } let person = new Person("Christoph"); person.sayName(); // logs "Christoph" console.log(person instanceof Person); // true console.log(person instanceof Object); // true
  6. Class Equivalent in ES6 let Person = (function() { "use

    strict"; const Person = function(name) { if (typeof new.target === "undefined") { throw new Error("Constructor must be called with new."); } this.name = name; } Object.defineProperty(Person.prototype, "sayName", { value: function() { console.log(this.name); }, enumerable: false, writable: true, configurable: true }); return Person; }());
  7. Constant Class Names class Foo { constructor() { Foo =

    "bar";// throws an error } } Foo = "baz"; // ¯\_(ϑ)_/¯
  8. “OOP to me means only messaging,
 local retention
 and protection

    and hiding of state-process,
 and extreme late binding of all things.” ― Alan Kay
  9. “A program execution is regarded as a physical model, simulating

    the behavior of either a real or imaginary part of the world.” ― Kristen Nygaard
  10. { }

  11. Creating new Objects let thePrototype = {}; let theObject =

    Object.create(thePrototype); let anotherObject = Object.create(theObject);
  12. Class Methods of String [:try_convert, :allocate, :new, :superclass, :freeze, :===,

    :==, :<=>, :<, :<=, :>, :>=, :to_s, :inspect, :i ncluded_modules, :include?, :name, :ancestors, :instan ce_methods, :public_instance_methods, :protected_insta nce_methods, :private_instance_methods, :constants, :c onst_get, :const_set, :const_defined?, :const_missing, :class_variables, :remove_class_variable, :class_varia ble_get, :class_variable_set, :class_variable_defined? , :public_constant, :private_constant, :singleton_clas .methods String
  13. Methods of a String [:<=>, :==, :===, :eql?, :hash, :casecmp,

    :+, :*,:%, : [], :[]=, :insert, :length, :size, :bytesize, :empty?, :=~, :match, :succ, :succ!, :next, :next!, :upto, :ind ex, :rindex, :replace, :clear, :chr, :getbyte, :setbyt e, :byteslice, :scrub, :scrub!, :freeze, :to_i, :to_f, :to_s, :to_str, :inspect, :dump, :upcase, :downcase, : capitalize, :swapcase, :upcase!, :downcase!, :capitali ze!, :swapcase!, :hex, :oct, :split, :lines, :bytes, : chars, :codepoints, :reverse, :reverse!, :concat, :<<, .methods String.new
  14. C B A + foo class A attr_reader :foo end

    class B < A end class C < B end thing = C.new thing.foo
  15. B Object BasicObject A + foo class A attr_reader :foo

    end class B < A end class C < B end thing = C.new thing.foo C B A + foo
  16. :thing # __proto__ :thing # __proto__ + foo let other

    = { bar: 42 }; :other # __proto__ + bar let thing = Object.create(other); thing.foo = "hello"; thing.foo thing.bar // "hello"
  17. :thing # __proto__ :thing # __proto__ + foo let other

    = { bar: 42 }; :other # __proto__ + bar let thing = Object.create(other); thing.foo = "hello"; thing.foo // "hello" thing.bar null // 42