用JavaScript创建新对象 [英] Creating New Objects in JavaScript

查看:48
本文介绍了用JavaScript创建新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于JavaScript中的面向对象编程,我还是一个相对较新的人,我不确定在JavaScript中定义和使用对象的最佳"方法.我已经看到了定义对象和实例化新实例的规范"方法,如下所示.

I'm a relatively newbie to object oriented programming in JavaScript, and I'm unsure of the "best" way to define and use objects in JavaScript. I've seen the "canonical" way to define objects and instantiate a new instance, as shown below.

function myObjectType(property1, propterty2) {
    this.property1 = property1,
    this.property2 = property2
}
// now create a new instance
var myNewvariable = new myObjectType('value for property1', 'value for property2');

但是我已经看到了以这种方式创建对象新实例的其他方法:

But I've seen other ways to create new instances of objects in this manner:

var anotherVariable = new someObjectType({
    property1:    "Some value for this named property",
    property2:    "This is the value for property 2"
});

我喜欢第二种方式的出现-代码是自我记录.但是我的问题是:

I like how that second way appears - the code is self documenting. But my questions are:

  1. 哪种方式更好"?

  1. Which way is "better"?

我可以使用第二种方式来 实例化对象的变量 使用 古典"的定义方式 具有隐式对象类型 构造函数?

Can I use that second way to instantiate a variable of an object type that has been defined using the "classical"way of defining the object type with that implicit constructor?

如果我想创建一个数组 这些物体,还有其他 考虑因素?

If I want to create an array of these objects, are there any other considerations?

谢谢.

推荐答案

这真是令人难受.这样:

It's really down to taste. This way:

var anotherVariable = new someObjectType({
    property1:    "Some value for this named property",
    property2:    "This is the value for property 2"
});

...如果参数多于2/3,通常会更好,因为它有助于提高可读性,并且更容易避免可选参数问题(fn(null,null,null,123')).

... is generally better if there's more than 2/3 arguments, as it aids readability and makes it easier to avoid the optional argument problem (fn(null,null,null,123')).

另一个要考虑的是性能.以常规方式传递参数会更快,但是这种速度增益只有在对性能非常敏感的情况下才有意义.

Another consideration is performance. Passing arguments in the conventional way will be faster, but this speed gain only becomes significant in very performance-sensitive situations.

我可以使用第二种方法来实例化一个对象类型的变量,该变量是通过使用该隐式构造函数定义对象类型的经典"方式定义的吗?

Can I use that second way to instantiate a variable of an object type that has been defined using the "classical"way of defining the object type with that implicit constructor?

不容易.如果您想使用散列而不是仅传递参数来实例化构造函数,并且您无法控制源,则可以包装"它:

Not easily. If you want to instantiate a constructor by using a hash instead of just passing arguments, and you don't have control over the source, then you could "wrap" it:

var _constructor = SomeConstructorFunction;

SomeConstructorFunction = function(hash) {
    return new _constructor(hash.property1, hash.property2);
};

尽管如此,我也不建议仅仅为了样式而与第三方API搞混.

I wouldn't really recommend messing with third-party APIs just for the sake of style though.

如果我想创建这些对象的数组,还有其他注意事项吗?

If I want to create an array of these objects, are there any other considerations?

数组有多大?数组到底是什么?性能可能值得考虑...

How big is the array? What's the array for exactly? Performance might be worth considering...

这篇关于用JavaScript创建新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆