OLOO (Objects Linking to Other Objects)

OLOO (Objects Linking to Other Objects)

Andrew Velez
Posted on 9/27/2019Last Modified: 4/7/2020


Recent Update has made it so this is as fast as Object.assign and Object.create so ignore what i say at the beginning of the video.


Video on how to use module, Why to use, and when to use.



Why would i use this?


Not having to bind to all the methods of a class. Some of my classes i had created for React require me to bind to 20+ methods and if i forget any it can cause a crash if i need access to “this” in the method.

It uses an Object.assign forked method that Microsoft recommends for when you use getters and setters on the Objects. If you are only going to be one level deep and don't care to inherit the methods from the base Object and you don't have getters or setter then Object.Assign would work just fine.

It also makes sure to keep the inheritance chain, which means if you inherit from 5 classes you can always call the base classes method.

Heres a short Stack Overflow Question on why to use OLOO over Prototype Design for Example. Oloo va Prototype Design

An even longer explanation is available in Kyle Simpsons book You Don’t Know JS on Objects.


How the module is implemented.


const Test = {
      create() {
        // the first Object Literal always uses Object.create since this allows to reuse function memory.
        const test = Object.create(Test);
        return test;
      },
      inheritedObject: 0,
      get meme() {

      },
      method1() {
        console.log('Test');
      }
    }

    const Test2 = {
      create() {
// Inherit from base class in a way that doesn’t call get and set like Object.assign would. 
        return Oloo.assign(Test.create(), Test2);
      },
      inherited2: 0,
      get meme() {
        return 0;
      },
      get meme2() {

      },
      method1() {
        console.log('Test2');
      }
    }

    const Test3 = {
      create() {
        return Oloo.assign(Test2.create(), Test3);
      },
      inherited3: 0,
      method1() {
        console.log('Test3');
      },
      method2() {
        // Calling method 1 on base class.
        Oloo.base(this, this.method1);
      }
    }

    // This works all properties need to be high level while funcitons are in prototype.
    const test = Test.create();
    const test2 = Test2.create();
    const test3 = Test3.create();
    test.method1();
    // "Test"
    test2.method1();
    // "Test2"
    test3.method1();
    // "Test3"
    test3.method2();
    // "Test2"

Andrew Velez
Posted on 9/27/2019Last Modified: 4/7/2020

...

Recomendations

Games for Sale

Comments 0

Display Name

Email


0 / 10