哪种方式最适合在 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 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.");
第二种方式:
var Robot = {
metal: "Titanium",
killAllHumans: function(){
alert("Exterminate!");
}
};
Robot.killAllHumans();
第三种方式 — 使用数组语法的JavaScript对象:
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.
- 如果您想创建多个相似的对象,请使用方式 1.在您的示例中,
Person
(名称应以大写字母开头)称为构造函数.这类似于其他面向对象语言中的类. - 如果您只需要一个对象(如单例),请使用方式 2.如果您希望此对象从另一个对象继承,那么您必须使用构造函数.
- 如果您想根据对象的其他属性初始化对象的属性,或者您有动态属性名称,请使用方式 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.
依赖属性:
以下内容不起作用,因为this
不引用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屋!