1

我正在努力学习面向对象的javascript。我有一个看起来像这样的对象构造函数:

//generic object
function myObject(value1, value2, value3){
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
this.method1 =method1;
}

function method1(){
alert('hello');
}

function method2(){
alert('hello again');
}

我正在创建这个对象的新实例,如下所示:

instance1 = new myObject(
1, //value1
2, //value2
3  //value3
)

我的问题是:在 myObject 的一些实例上,我需要自定义值和方法。我想在创建时添加它们。但是如果不再次编写实例名称,我无法弄清楚如何做到这一点,我试图避免这种情况,因为它会显着减慢添加新对象的过程。

有什么方法可以替换此代码..

 instance1 = new myObject(
 1, //value1
 2, //value2
 3  //value3
 )
 instance1.method2 =method2;
 instance1.value4 =4;

..不需要重复名称“instance1”的东西?

4

3 回答 3

5

您可以在 MyObject 上创建更新功能prototype

function MyObject(value1, value2, value3){
    this.value1 = value1;
    this.value2 = value2;
    this.value3 = value3;
    this.method1 =method1;
}

MyObject.prototype.update = function( args ) {
    for(var name in args) {
        this[ name ] = args[ name ];
    }
};

//using it
var instance1 = new MyObject( 1, 2, 3 );
instance1.update( {
    method2: method2,
    value4: 4
});

需要注意的是,我将构造函数的名称从myObject更改为MyObject。在 JavaScript 中,构造函数的名称是帕斯卡大小写而不是驼峰大小写,这是一种约定。这样,当开发人员看到一个帕斯卡大小写的函数时,他们知道他们需要使用new关键字。你可以在这里阅读更多:http: //elegantcode.com/2010/12/21/basic-javascript-part-4-enforcing-new-on-constructor-functions/

Learn jQuery and JavaScript我还建议作为新的 JS 开发人员从 AppendTo.com http://learn.appendto.com/查看这个免费系列

于 2012-08-22T01:01:28.437 回答
2

Since you have jQuery available, you can use jQuery.extend() and avoid having to retype instance1. jQuery.extend() copies properties from one object to another.

jQuery.extend(instance1, {
    method2: method2
    value4: 4
});

This will copy all the properties of the second argument to the object in the first argument. See the jQuery doc for more info.


If you wanted to specify this as part of the constructor, you could do so like this and not have any additional lines of code to type:

function myObject(value1, value2, value3, extraProperties){
    this.value1 = value1;
    this.value2 = value2;
    this.value3 = value3;
    this.method1 = method1;
    if (extraProperties) {
        jQuery.extend(this, extraProperties);
    }
}

In that case, you would just code this:

var instance1 = new myObject(1, 2, 3, {method2: method2, value4: 4});

Of course, the more Object Oriented way to do this would be to create prototypes for each type of object you want (some could inherit from others) and then just instantiate the right type of object rather than add instance-specific methods and properties.

Or, create a single prototype that has all the methods/capabilities you might need on it for all the uses of the object rather than adding instance specific methods. In javascript, there is no penalty for having methods on an object that you don't use sometimes if they are on the prototype. Things on the prototype are free per instance (no storage consumption per instance, no initialization cost per instance, etc...).

于 2012-08-22T01:06:18.717 回答
0
function myObject (value1, value2, value3, attributes)
{
    $.extend(this, attributes);
    // the rest of your code
}

// Usage
var obj = new myObject(1,1,1,{ abc: 123, something: "nothing" });

注意:这不会是全局的。Aka 如果您制作 2​​ 个具有不同属性值的对象,那么它们每个都有自己的自定义属性。如果您想让这些属性影响每个对象,请查看调整它的原型。

于 2012-08-22T01:09:03.223 回答