hide the getter or add in the proto Object

会有一股神秘感。 提交于 2019-12-13 03:38:58

问题


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 }


回答1:


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 })



回答2:


Those __defineGetter__, __defineSetter__, __lookupGetter__ and __lookupSetter__ properties you see there have nothing to do with your sprite getter. They are just native (but long deprecated) methods of Object.prototype (which is the object that you have expanded in your view). There's nothing you can do against them.



来源:https://stackoverflow.com/questions/48891874/hide-the-getter-or-add-in-the-proto-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!