0

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

jon
  • 1,494
  • 3
  • 16
  • 29
  • `so that it does not disturb my eyes in a debug terminal?` ... Maybe use another IDE or sth? – Jonas Wilms Feb 20 '18 at 18:34
  • Uh, don't look at `Object.prototype` (don't expand it in your view) if you don't want to see it?! – Bergi Feb 20 '18 at 18:38
  • ok sorry picture are not clear, its "get sprite() { return this.slot.currentSprite }" i need hide – jon Feb 20 '18 at 18:52
  • 1
    There are plenty of valid and sane reasons for wanting to move some functionality up the prototype chain, but "so that it does not disturb my eyes in a debug terminal" is not one of them. – Patrick Roberts Feb 20 '18 at 18:59
  • I try to stay clean, because I sometimes lose a lot of time to visualize a property or a method. Especially when I have to come back after several weeks. I'm 100% agree with you, but the problem is that I can not install theme on this version of node.js – jon Feb 20 '18 at 19:04

2 Answers2

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 })
Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • these objects are created during a loading phase, they will not affect performance. – jon Feb 20 '18 at 19:34
  • @jon every time you access `buttonsData[name].sprite`, the property access is evaluated slower because the accessor exists higher up the `prototype` chain, so it takes longer to search for. Yes, there is a deterioration in performance. – Patrick Roberts Feb 20 '18 at 19:35
  • yes, I understand thank you, however its for an simple external module, a map editor. I have a lot of performance game because most of the big calculations are done by the GPU. I can cheat a little somewhere. – jon Feb 20 '18 at 19:43
  • 1
    @jon I have to apologize, but apparently this approach has a 14x speed increase. I'm still scratching my head. – Patrick Roberts Feb 20 '18 at 19:48
  • 1
    @jon I wrote [a question](https://stackoverflow.com/q/48893360/1541563) about it. Someone dug [this](https://stackoverflow.com/a/42709301/1541563) up, which suggests that it's due to an implementation issue in V8 with getters and setters. In other engines like Firefox's, there is no performance difference between the two. – Patrick Roberts Feb 20 '18 at 20:29
  • ok, pity that your test on jsbench don't make test on all Engine. Like jsperf.com can do , do you have a bench on jsperf.com ? – jon Feb 20 '18 at 20:48
  • @jon The sites work the same way. The tests both rely on the user to open the browser they want to test with. Adding it to jsperf would provide no additional information. – Patrick Roberts Feb 20 '18 at 20:50
  • its give a browserscope after test. http://www.browserscope.org/user/tests/table/ahBzfnVhLXByb2ZpbGVyLWhycg0LEgRUZXN0GMihuwQM – jon Feb 20 '18 at 20:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165519/discussion-between-patrick-roberts-and-jon). – Patrick Roberts Feb 20 '18 at 20:53
0

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • ok sorry picture are not clear, its "get sprite() { return this.slot.currentSprite }" i need hide – jon Feb 20 '18 at 18:47
  • I don't think you can do that. You might want to try `Object.defineProperty(…, "sprite", { enumerable: false })`, but I'm not sure how that will affect the console view. – Bergi Feb 20 '18 at 19:07
  • thank you a lot for you help, but @Patrick Roberts give me the correct ways. – jon Feb 20 '18 at 19:58