我正在试验ES6中的代理,试图在对象上创建无限可链接的代理(从https://jsonplaceholder.typicode.com/users获得),如果找不到prop,则返回空{}
。在嵌套对象上实现代理
我试图实现此功能高达第二级(例如user.address.geo)。 编辑:用于检查类型属性内容的更新代码
let users = [{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "[email protected]",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874"
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org"
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "[email protected]",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771"
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net"
}];
我想出了下面的代码
var handler = {
get: function (target, name) {
return name in target ?
target[name] : {};
}
};
let pusers = users.map(item => {
let pitem = new Proxy(item, handler);
Reflect.ownKeys(pitem).map(prop => {
pitem[prop] = (typeof pitem[prop] == 'object') ? new Proxy(pitem[prop], handler) : pitem[prop];
})
return pitem;
});
pusers.map(u => {
console.log(u.address);
console.log(u.contact.city)
});
这段代码的输出是没有吸引力,它返回undefined
,而不是一个空{}
对象
{ street: 'Kulas Light',
suite: 'Apt. 556',
city: 'Gwenborough',
zipcode: '92998-3874' }
undefined
我做了这几次,仍然得到相同的结果。我错过了什么吗?
[有没有这样的事,作为一个“JSON对象”](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) –
'checkJSON' - 什么?这看起来非常令人费解,根本不清楚该功能应该做什么。 – Bergi
检查值是否是类型对象:我从http://stackoverflow.com/questions/4295386/how-can-i-check-if-a-value-is-a-json-object – NikhilGoud