Search code examples
javascriptdebuggingobjectgetter-setter

hide the getter or add in the proto Object


is a way how i can create a getter, but hide it or add it in proto ? example here, i use simple OBJ. Look get sprite()

    buttonsData[name] = {
        name:name,
        type:bType,
        slot:slot,
        get sprite() { return this.slot.currentSprite },
    };

but I find it very polluting, how can I hide it or write it so that it does not disturb my eyes in a debug terminal? i want hide get sprite() { return this.slot.currentSprite } enter image description here


Solution

  • You can embed an anonymous prototype using Object.create(), though do note that this slightly deteriorates drastically improves performance for no reason other than "it disturbs my eyes in a debug terminal".

    I do not advocate the use of this approach in performance-critical code How...

    buttonsData[name] = Object.assign(Object.create({
        get sprite() { return this.slot.currentSprite }
    }), {
        name: name,
        type: bType,
        slot: slot
    });
    

    This creates an object like this:

    {
      name: "Spines",
      slot: Slot,
      type: "sheetsType",
      __proto__: {
        get sprite: function () {...},
        __proto__: Object
      }
    }
    

    For re-usability, it would probably be better to implement a class instead of creating an anonymous prototype for each object added to buttonsData:

    class ButtonsData {
      constructor (data = {}) {
        Object.assign(this, data)
      }
    
      get sprite () { return this.slot.currentSprite }
    }
    

    And use it like this:

    buttonsData[name] = new ButtonsData({ name, type: bType, slot })