隐藏 getter 或添加原型对象
hide the getter or add in the proto Object
是我如何创建 getter,但将其隐藏或添加到 proto 中的一种方式?
这里的例子,我使用简单的 OBJ。
看get sprite()
buttonsData[name] = {
name:name,
type:bType,
slot:slot,
get sprite() { return this.slot.currentSprite },
};
但我觉得它很污染,我怎样才能隐藏它或写它,以免它在调试终端打扰我的眼睛?
我想隐藏 get sprite() { return this.slot.currentSprite }
您在此处看到的 __defineGetter__
、__defineSetter__
、__lookupGetter__
和 __lookupSetter__
属性 与您的 无关=14=] getter。它们只是 Object.prototype
(您在视图中展开的对象)的本机(但长期弃用)方法。你对他们无能为力。
您可以使用 Object.create()
, though do note that this slightly deteriorates drastically improves 性能嵌入匿名 prototype
,除了 "it disturbs my eyes in a debug terminal"。
我不提倡在performance-critical代码中使用这种方式How...
buttonsData[name] = Object.assign(Object.create({
get sprite() { return this.slot.currentSprite }
}), {
name: name,
type: bType,
slot: slot
});
这会创建一个这样的对象:
{
name: "Spines",
slot: Slot,
type: "sheetsType",
__proto__: {
get sprite: function () {...},
__proto__: Object
}
}
对于 re-usability,最好实现一个 class
而不是为每个添加到 buttonsData
的对象创建一个匿名 prototype
:
class ButtonsData {
constructor (data = {}) {
Object.assign(this, data)
}
get sprite () { return this.slot.currentSprite }
}
并像这样使用它:
buttonsData[name] = new ButtonsData({ name, type: bType, slot })
是我如何创建 getter,但将其隐藏或添加到 proto 中的一种方式? 这里的例子,我使用简单的 OBJ。 看get sprite()
buttonsData[name] = {
name:name,
type:bType,
slot:slot,
get sprite() { return this.slot.currentSprite },
};
但我觉得它很污染,我怎样才能隐藏它或写它,以免它在调试终端打扰我的眼睛?
我想隐藏 get sprite() { return this.slot.currentSprite }
您在此处看到的 __defineGetter__
、__defineSetter__
、__lookupGetter__
和 __lookupSetter__
属性 与您的 无关=14=] getter。它们只是 Object.prototype
(您在视图中展开的对象)的本机(但长期弃用)方法。你对他们无能为力。
您可以使用 Object.create()
, though do note that this slightly deteriorates drastically improves 性能嵌入匿名 prototype
,除了 "it disturbs my eyes in a debug terminal"。
我不提倡在performance-critical代码中使用这种方式How...
buttonsData[name] = Object.assign(Object.create({
get sprite() { return this.slot.currentSprite }
}), {
name: name,
type: bType,
slot: slot
});
这会创建一个这样的对象:
{
name: "Spines",
slot: Slot,
type: "sheetsType",
__proto__: {
get sprite: function () {...},
__proto__: Object
}
}
对于 re-usability,最好实现一个 class
而不是为每个添加到 buttonsData
的对象创建一个匿名 prototype
:
class ButtonsData {
constructor (data = {}) {
Object.assign(this, data)
}
get sprite () { return this.slot.currentSprite }
}
并像这样使用它:
buttonsData[name] = new ButtonsData({ name, type: bType, slot })