哪种方式最适合在JavaScript中创建对象?在对象属性之前是否需要`var`? [英] Which way is best for creating an object in JavaScript? Is `var` necessary before an object property?
问题描述
到目前为止,我看到了在JavaScript中创建对象的三种方法。哪种方式最适合创建对象以及为什么?
So far I saw three ways for creating an object in JavaScript. Which way is best for creating an object and why?
我还在所有这些示例中看到关键字 var
不是在财产之前使用 - 为什么?是否没有必要在属性名称之前声明 var
,因为它提到属性是变量?
I also saw that in all of these examples the keyword var
is not used before a property — why? Is it not necessary to declare var
before the name of a property as it mentioned that properties are variables?
In在第二种和第三种方式中,对象的名称是大写的,而在第一种方式中,对象的名称是小写的。我们应该在什么情况下使用对象名称?
In the second and third way, the name of the object is in upper-case whereas in the first way the name of the object is in lower-case. What case should we use for an object name?
function person(fname, lname, age, eyecolor){
this.firstname = fname;
this.lastname = lname;
this.age = age;
this.eyecolor = eyecolor;
}
myFather = new person("John", "Doe", 50, "blue");
document.write(myFather.firstname + " is " + myFather.age + " years old.");
第二种方式:
Second way:
var Robot = {
metal: "Titanium",
killAllHumans: function(){
alert("Exterminate!");
}
};
Robot.killAllHumans();
第三种方式 - 使用数组语法的JavaScript对象:
Third way — JavaScript objects using array syntax:
var NewObject = {};
NewObject['property1'] = value;
NewObject['property2'] = value;
NewObject['method'] = function(){ /* function code here */ }
推荐答案
没有最好的方式,这取决于你的用例。
There is no best way, it depends on your use case.
- 如果要创建多个类似对象,请使用 way 1 。在您的示例中,
Person
(您应该以大写字母开头的名称)称为构造函数。这与其他OO语言中的 classes 类似。 - 如果您只需要一个对象,请使用 way 2 >一种(像单身人士)。如果你希望这个对象继承自另一个,那么你必须使用构造函数。
- 如果你想初始化属性,可以使用 way 3 对象取决于它的其他属性或是否有动态属性名称。
- Use way 1 if you want to create several similar objects. In your example,
Person
(you should start the name with a capital letter) is called the constructor function. This is similar to classes in other OO languages. - Use way 2 if you only need one object of a kind (like a singleton). If you want this object to inherit from another one, then you have to use a constructor function though.
- Use way 3 if you want to initialize properties of the object depending on other properties of it or if you have dynamic property names.
更新:如请求的示例第三种方式。
Update: As requested examples for the third way.
依赖属性:
以下不能用作此
不是否参考 book
。无法使用对象文字中的其他属性值初始化属性:
The following does not work as this
does not refer to book
. There is no way to initialize a property with values of other properties in a object literal:
var book = {
price: somePrice * discount,
pages: 500,
pricePerPage: this.price / this.pages
};
相反,你可以这样做:
var book = {
price: somePrice * discount,
pages: 500
};
book.pricePerPage = book.price / book.pages;
// or book['pricePerPage'] = book.price / book.pages;
动态属性名称:
如果属性名称存储在某个变量中或通过某个表达式创建,然后您必须使用括号表示法:
If the property name is stored in some variable or created through some expression, then you have to use bracket notation:
var name = 'propertyName';
// the property will be `name`, not `propertyName`
var obj = {
name: 42
};
// same here
obj.name = 42;
// this works, it will set `propertyName`
obj[name] = 42;
这篇关于哪种方式最适合在JavaScript中创建对象?在对象属性之前是否需要`var`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!