Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过Javascript中的任意属性创建具有无限深度的对象?_Javascript - Fatal编程技术网

如何通过Javascript中的任意属性创建具有无限深度的对象?

如何通过Javascript中的任意属性创建具有无限深度的对象?,javascript,Javascript,我只是好奇,但我如何在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 }

我只是好奇,但我如何在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
}
这件事可以用你的钱来完成

让_void={};
_void=newproxy(_void,{get:void=>the_void});
console.log(无效);//代理{}
console.log(the_void.this);//代理{}
console.log(_void.this.is);//代理{}
console.log(_void.this.is.interest);//代理{}
console.log(_void.this.is.interest.is.t);//代理{}
console.log(_void.this.is.interest.is.t.it);//代理{}
console.log(_void.this.is.interest.is.t.it['?']);//代理{}
可以使用

让_void={};
_void=newproxy(_void,{get:void=>the_void});
console.log(无效);//代理{}
console.log(the_void.this);//代理{}
console.log(_void.this.is);//代理{}
console.log(_void.this.is.interest);//代理{}
console.log(_void.this.is.interest.is.t);//代理{}
console.log(_void.this.is.interest.is.t.it);//代理{}
console.log(_void.this.is.interest.is.t.it['?']);//代理{}
递归

var a = {};
var cur_level = 1;
function depth(obj,level = 0){
    if (cur_level == level) {
         return
    } else {
        obj.new_level = {};
        cur_level++;
        obj.val = Math.random(); //save some data
        return depth(obj.new_level,level); //make new level
    }
}
depth(a,100) // in this case if you using this declare u receive depth near 100 levels
//depth(a) -> in this case you will receive infinite depth of your obj
console.log(a)
递归的例子

var a = {};
var cur_level = 1;
function depth(obj,level = 0){
    if (cur_level == level) {
         return
    } else {
        obj.new_level = {};
        cur_level++;
        obj.val = Math.random(); //save some data
        return depth(obj.new_level,level); //make new level
    }
}
depth(a,100) // in this case if you using this declare u receive depth near 100 levels
//depth(a) -> in this case you will receive infinite depth of your obj
console.log(a)

您可以使用递归函数@GetOffMyLawn的可能重复。这个问题是询问如何创建对象,而不是如何循环。还有什么创造性的方法来实现它吗?你可以使用递归函数@GetOffMyLawn的可能重复。这个问题是问如何创建对象,而不是如何循环。还有其他创造性的方法吗?
depth(a)
给出了
未捕获的范围错误:超过了最大调用堆栈大小,因为它需要无限递归函数调用。它是无限循环,没有结束:)不要运行它。我编辑代码是为了更简单。
depth(a)
给出了
Uncaught RangeError:超过了最大调用堆栈大小,因为它需要无限递归函数调用。它是无限循环,没有结束:)不要运行它。我编辑代码是为了更简单。