简体   繁体   English

我可以在JavaScript的对象中包含对象吗?

[英]Can I have objects inside an object in JavaScript?

So I want to have objects in objects eg 所以我想在对象中包含对象

 1. var Parent={
 2.    var Child1 = {
 3.       funct:function(){
 4.          ...
 5.       },
 6.       funct2:function(){
 7.          ...
 8.       }
 9.    },
 10.   var Child2 = {
 11.      funct3:function(){
 12.         ...
 13.      }
 14.   }
 15. }

so I can do something like Parent.Child1.funct() ...etc. 所以我可以做类似Parent.Child1.funct()...之类的事情。 Is there a way to do this? 有没有办法做到这一点?

Yes, but you'll need to respect the object syntax. 是的,但是您需要遵守对象语法。

var Parent = {
    Child1: {
        funct: function() {
        }
    },
    Child2: {
        funct: function() {
        }
    }
};

Sure, it's just another property: 当然,这只是另一个属性:

var Parent={
  Child1: {
    funct: function() {}
  },
  Child2: {
    funct: function() {}
  }
}

Parent.Child1.funct();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迭代 Javascript 中的对象数组 - 为什么我不能获取当前 object 内部的增量块? - Iterating over an array of objects in Javascript - why can't I obtain current object inside for increment block? 如何访问对象函数(javascript)中的objects变量? - How can i access the objects variable inside an objects function (javascript)? 您能解释一下JSON对象如何存储在Javascript对象中吗? - Can you explain how JSON objects are stored inside a Javascript object? 我可以在JavaScript的方法内添加对象吗? - Can i add a object inside a method in JavaScript? 带有内部对象的javascript json对象 - javascript json object with objects inside 如何在 JavaScript 中的另一个对象中找到具有属性的对象 - How do I find objects with a property inside another object in JavaScript 如何在对象数组 Javascript 中过滤数组 object? - How do I filter an array object inside of a array of objects Javascript? 如何验证 2 个 JavaScript 对象是否具有相同的键 - How can I verify if 2 JavaScript Objects have the same keys or not 如何检查两个javascript对象是否具有相同的字段? - How can I check if two javascript objects have the same fields? 我可以在JavaScript中使用自定义toString的对象数组吗? - Can I have an array of objects with a custom toString in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM