如何在Javascript中创建对象内部的对象

How to create object inside object in Javascript

本文关键字:内部 对象 创建对象 Javascript      更新时间:2023-09-26

我正在尝试创建一个对象,其中的每个参数都是另一个对象:

var divTextPerScreenWidthMap = new Object(
       {'360','click'},
       {'480','click it'},
       {'768','click it right'},
       {'1024','you know you want to click it'},
       {'1280','click this button which is very long will help you'}
    );

这不起作用,因为我遇到了一个错误。我需要如何写才能使它工作?我应该将外部对象更改为数组吗?如何更改?

您有语法错误。

首先,对象文字遵循以下语法:

var literal = {
    "Name": "value",
    "Array": [],
    "NestedObject": {}
};

名称值分隔符是冒号,而不是逗号。

编辑

上面的代码可以重写如下

// declaration via array initializer
var myArray = [
   // name : value syntax
   {'360': 'click'},
   // values separated by comma
   {'480': 'click it'},
   {'768': 'click it right'},
   {'1024': 'you know you want to click it'},
   {'1280': 'click this button which is very long will help you'}
]

然而,在这一点上,你不能通过这样的名字访问你的对象:

var firstObject = myArray[0];
// will throw an error
firstObject.360 = "not click";

您只能按照以下使用它

firstObject["360"] = "not click";

因此,我建议您根据变量命名规则来命名属性。

在javascript中,对象是一个简单的映射。最好使用文字{},而不是od-newObject();

  var myObj = {      
     prop : {},
     prop2 : {} 
   }

不要通过构造函数创建Object,请使用字面语法{}

此外,对象具有关键点和特性。您的对象似乎只有值。你是想用Array吗?

您完全忘记了为值提供键。如果您不想使用密钥,请使用数组:

var divFoo = [
  [360, "click"],
  [480, "click it"] // et cetera
];

这将为您提供一个数组数组。例如,divFoo[0][0] == 360

如果你想要钥匙,请使用一个对象:

var divFoo = {
  "360": "click",
  "480": "click" // et cetera
}

这会给你一个简单的对象。divFoo[360] == "click"

或者,你可以使用一组对象来提高描述性:

var divFoo = [
  {time: 360, text: "click"},
  {time: 480, text: "click it"} // et cetera
];

在这种情况下,divFoo[1].text == "click it"

此外,一些提示:

  • 不要使用new Objectnew Array。它们是多余的
  • 如果将整数用作值,则无需引用整数

将对象集合表示为数组是有意义的:

var divTextPerScreenWidthMap = [
           {360:'click'},
           {480:'click it'},
           {768:'click it right'},
           {1024:'you know you want to click it'},
           {1280:'click this button which is very long will help you'}
        ];
//You could iterate over your array of objects with a for loop:
        
      var i, value;
      for (i=0; i < divTextPerScreenWidthMap.length; i++) {
        value = divTextPerScreenWidthMap[i];
        console.log(value);
      };
//Alternatively, you could represent your data structure as one object:
    var divTextPerScreenWidthMap = {
           360:'click',
           480:'click it',
           768:'click it right',
           1024:'you know you want to click it',
           1280:'click this button which is very long will help you'
        };
    
//So now if you have a screen width, you can quickly get back the corresponding message:
    var screenWdith = 360;
    alert(divTextPerScreenWidthMap[screenWidth]);

您还可以创建这样的嵌套对象:

let obj1 = {};
obj1['title'] = 'Vehicles';
let obj2 = {};
obj2['name'] = 'Honda City';
obj1['cars'] = obj2;
console.log(obj1);

在对象中创建一个方法,在该方法中创建一个子对象并返回。

const obj = {
  one: "one is just a property",
  two: function(){
    const newObject = {
      three: "now two will return new a new object"
    }
    return newObject;
  }
}