我只是很好奇,但是如何在 Javascript 中通过任意属性创建一个具有无限深度的对象?这将是一件有趣的事情。
console.log(a); // is an object
for(let depth = 1; ; depth++)
{
const arbitrary_property_name = Math.random().toString(36);
console.log(a = a[arbitrary_property_name]); // is also an object
}
可以用Proxy来完成。
let the_void = {};
the_void = new Proxy(the_void, {get: _ => the_void});
console.log(the_void); // Proxy {}
console.log(the_void.this); // Proxy {}
console.log(the_void.this.is); // Proxy {}
console.log(the_void.this.is.interesting); // Proxy {}
console.log(the_void.this.is.interesting.isn.t); // Proxy {}
console.log(the_void.this.is.interesting.isn.t.it); // Proxy {}
console.log(the_void.this.is.interesting.isn.t.it['?']); // Proxy {}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句