-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
有时候setter和getter是有逻辑的,但是又希望可以利用oo隐藏掉具体的属性强制只能使用getter访问,是否有可能实现类似的功能。
有2个想法,第一种是静态方法:
exports.setFoo = function (value) {
value.bar = true;
oo.set(this, 'foo', value);
};
exports.getFoo = function () {
var value = oo.get(this, 'foo');
value.bar = false;
return value;
};第二种是hook模式:
oo.defineAccessor(
exports,
'foo',
{
getHook: function (value) {
// value是oo拿到的
value.bar = false;
return value; // 返回给外面
},
setHook: function (value) {
// value是传进来的
value.bar = true;
return value; // 随后会被真正set
}
}
);Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels