JavaScript 对象嵌套

标签 javascript jquery json module

我昨天在这里工作,将一些旧的意大利面条 jquery 代码转换为模块化模式,然后我开始思考这个:

我该怎么做这样的事情? 这可能吗?

var obj = {
    insideObj1: {
        moreInsideObj1: {
            evenMoreInsideObj1: {
            	//How could I reference the "moreInsideObj1" from here
            },
            evenMoreInsideObj2: {
            	//How could I reference the "insideObj1" from here
            },
            evenMoreInsideObj2: {
            	//How could I reference the "obj" from here
            }

        },
        moreInsideObj2: {
        	//How could I do the same thing here
        },
    },
    insideObj2: {
    	//Here I can use "this.insideObj1" right;
    }
};

**我是 JS 和 JQuery 世界的新手。

像这样的事情怎么样:

var obj = (function(){
    insideObj1: {
        moreInsideObj1: {
            evenMoreInsideObj1: {
            	//How could I reference the "moreInsideObj1" from here
            },
            evenMoreInsideObj2: {
            	//How could I reference the "insideObj1" from here
            },
            evenMoreInsideObj2: {
            	//How could I reference the "obj" from here
            }

        },
        moreInsideObj2: {
        	//How could I do the same thing here
        },
    },
    insideObj2: {
    	//Here I can use "this.insideObj1" right;
    }
})();

最佳答案

很容易:

var a = obj.insideObj1.moreInsideObj1.evenMoreInsideObj1;
var b = obj.insideObj1.moreInsideObj1.evenMoreInsideObj2;
var c = obj.insideObj1.moreInsideObj1.evenMoreInsideObj2;
var d = obj.insideObj1.moreInsideObj2;

只需添加另一个点即可更深。

关于JavaScript 对象嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39900161/

相关文章:

javascript - Tempus-dominus 和 sweetalert2

javascript - 黑色边框 div 形状

javascript - WordPress JSON API - 请求 header 错误

javascript - Node js回调函数

php - 发送 header 后关闭网页(php)

java - 为什么我收到消息 : "Unhandled event loop exception Java heap space" in Eclipse when using javascript autocomplete?

javascript - 当 Jquery 在到达 anchor 时编辑我的 CSS 时如何更改?

javascript - 使用 JS 代码的移动响应

json - 如何为以下 JSON 数据创建模型类并解析它?

json - Spray-json 递归 json 问题 - 找不到证据参数的隐式值