隐藏吸气剂或添加原型对象 [英] hide the getter or add in the proto Object
问题描述
是一种方法,我可以创建一个吸气剂,但将其隐藏或添加到原型中? 在这里的例子中,我使用简单的OBJ. 看起来获取sprite()
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 },
};
但是我发现它非常污染,如何隐藏或编写它,以免在调试终端中干扰我的视线? 我要隐藏 get sprite(){返回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 }
推荐答案
您可以使用Object.create()
,但请注意,此会稍微恶化 的性能.
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".
我不主张在性能要求严格的代码中使用这种方法 如何...
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
}
}
对于可重用性,最好为每个添加到buttonsData
的对象实现class
而不是创建匿名prototype
:
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 }
}
并像这样使用它:
buttonsData[name] = new ButtonsData({ name, type: bType, slot })
这篇关于隐藏吸气剂或添加原型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!