-
Notifications
You must be signed in to change notification settings - Fork 17
Open
Description
我们需要让uioc支持ES5通过Object.defineProperty定义的setter,一个setter的判断方法比较简单:
var descriptor = Object.getOwnPropertyDescriptor(obj, propertyName);
return descriptor && descriptor.set一个问题是有可能一个对象同时有foo这个setter又有setFoo这个方法,我的建议是都注入一次
同时要支持auto的分析能把setter也找出来,这个逻辑也不难,配合Object.getOwnPropertyDescriptor就行了
我想周一是不是有可能完成这个功能,包括线下沟通的因为ES6 class默认成员不能枚举导致的for .. in失效问题
这里是一个已经写完的代替for .. in的方法
Setter.prototype.findAllSetters = function(config, instance) {
var exclude = config.properties || {};
var deps = [];
if (Object.getPrototypeOf && Object.getOwnPropertyDescriptors) {
var prototype = Object.getPrototypeOf(instance);
while (prototype) {
var keys = Object.getOwnPropertyKeys(keys);
for (var i = 0, len = keys.length; i < len; i++) {
var prop = this.getPropertyFromSetter(keys[i]);
// 有属性,未和属性配置冲突,且组件已注册
prop && !u.hasOwn(exclude, prop) && this.context.hasComponent(prop) && deps.push(prop);
}
prototype = Object.getPrototypeOf(prototype);
}
}
else {
for (var k in instance) {
if (typeof instance[k] === 'function') {
var prop = this.getPropertyFromSetter(k);
// 有属性,未和属性配置冲突,且组件已注册
prop && !u.hasOwn(exclude, prop) && this.context.hasComponent(prop) && deps.push(prop);
}
}
}
};可以在这个基础上改造加上对Object.getOwnPropertyDesriptor的使用,并将返回值由字符串改为{proptyName, setterType = 'accessor | method'应该能解决问题
另外有一个很关键的需要注意的是,因为有getter的存在,我们要尽量避免访问一个属性,因为getter带有逻辑可能产生一些副作用
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels