Javascript 为已知键创建空对象或具有空值的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33726401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create empty object or object with null values for already known keys
提问by albert
I am playing around with JavaScript and AngularJS since a few days and I am not sure about the following:
几天以来我一直在玩 JavaScript 和 AngularJS,但我不确定以下内容:
I am working on a sample project in which a user is able to create a new car with just a few properties stored in an JS object like this:
我正在开发一个示例项目,在该项目中,用户能够创建一辆新车,其中只包含一些存储在 JS 对象中的属性,如下所示:
var car = {
color: 'black',
seating: 'leather',
fuelconsumption: 'moderate'
}
The user can create a new car by clicking on an add-button. Angular then creates a new instance of a car and the user can specify the wishes for new car.
用户可以通过单击添加按钮来创建新车。然后 Angular 创建一个新的汽车实例,用户可以指定对新车的愿望。
However, should I create an empty object like
但是,我应该创建一个空对象,例如
var car = {}
and add the needed keys using the bound text inputs or should I do something different?
并使用绑定文本输入添加所需的键还是我应该做一些不同的事情?
Since the properties of a new car are known a-priori I could also create an object with the needed keys and null
as their values like this:
由于新车的属性是先验已知的,因此我还可以创建一个具有所需键null
及其值的对象,如下所示:
var car = {
color: null,
seating: null,
fuelconsumption: null
}
Which way would be more preferable due to e.g. best-practise?
由于最佳实践,哪种方式更可取?
回答by jfriend00
In Javascript, there is often not a need to initialize a property to null
. This is because referencing a property that has not been initialized just harmlessly returns undefined
which can work just as well as null
. This is obviously different than some other languages (Java, C++) where properties of an object must be declared before you can reference them in any way.
在 Javascript 中,通常不需要将属性初始化为null
. 这是因为引用尚未初始化的属性只是无害地返回undefined
,它可以像null
. 这显然与其他一些语言(Java、C++)不同,在这些语言中,必须先声明对象的属性,然后才能以任何方式引用它们。
What I would think might make sense in your case is to create a constructor function for creating a car
object because you will presumably be creating more than one car and all possible car objects won't be known at the time you write the code. So, you create a constructor function like this:
我认为在您的情况下可能有意义的是创建一个用于创建car
对象的构造函数,因为您可能会创建不止一辆汽车,并且在您编写代码时将不知道所有可能的汽车对象。所以,你创建了一个这样的构造函数:
function Car(color, seating, fuel) {
this.color = color;
this.seating = seating;
this.fuelConsumption = fuel;
}
Then, you can create a new Car object like this:
然后,您可以像这样创建一个新的 Car 对象:
var c = new Car('black', 'leather', 'moderate');
console.log(c.color); // 'black'
回答by Iliyan Yotov
You can do it both ways. I prefer creating the empty object using {}
and then adding the needed props but you can make it by defining the props with the initialization of the value:
你可以两种方式都这样做。我更喜欢使用创建空对象{}
,然后添加所需的道具,但您可以通过使用值的初始化定义道具来实现:
var car = {};
or
或者
var car = {
color: null,
seating: null,
fuelconsumption: null
};
Just like you did. I dont think there is a best practise for doing this. But maybe the values shoud point the needed type of the data saved this property.
就像你做的那样。我不认为有这样做的最佳实践。但也许这些值应该指向保存此属性的所需数据类型。
Example:
例子:
var car = {
color:"",
seating: "",
fuelconsumption: ""
};
In you case "" is fine.
在你的情况下,“”很好。
If using number NaN, undefined
.
如果使用 number NaN, undefined
。
If using strings ""
.
如果使用字符串""
。
If using objects or arrays {} []
.
如果使用对象或数组{} []
。
If using some kind of boolen values true/false
如果使用某种布尔值 true/false