Javascript 如何向对象的实例添加方法

Javascript 如何向对象的实例添加方法,javascript,Javascript,例如,我有一个对象的实例: A = function(){}; a = new A(); 如何添加方法 {b: function(){ console.log('b') }, c: function(){ console.log('c') } } 要举例说明?您应该看看原型 这是一个很好的解释 编辑:您还可以将原型设置为函数数组,如下所示: var Person = new function() {}; methods = { "methodA": function() {ale

例如,我有一个对象的实例:

A = function(){};
a = new A();
如何添加方法

{b: function(){ console.log('b') },
 c: function(){ console.log('c') }
}

要举例说明?

您应该看看
原型

这是一个很好的解释

编辑:您还可以将原型设置为函数数组,如下所示:

var Person = new function() {};

methods = {
    "methodA": function() {alert("method A")},
    "methodB": function() {alert("method B")},                         
}

Person.prototype = methods

p = new Person()
p.methodA(); // Alerts "method A"

您应该看看
原型

这是一个很好的解释

编辑:您还可以将原型设置为函数数组,如下所示:

var Person = new function() {};

methods = {
    "methodA": function() {alert("method A")},
    "methodB": function() {alert("method B")},                         
}

Person.prototype = methods

p = new Person()
p.methodA(); // Alerts "method A"

Prototype用于向特定类型对象的所有实例添加方法(对内存管理有用)。如果只想将方法添加到对象的一个实例,则可以像添加任何属性一样添加方法:

var A = function() {
    //......
}

var myA = new A();
myA.methodOne = function() { console.log('called methodOne on myA'); }
myA.methodTwo = function() { console.log('called methodTwo on myA'); }

myA.methodOne();
myA.methodTwo();

Prototype用于向特定类型对象的所有实例添加方法(对内存管理有用)。如果只想将方法添加到对象的一个实例,则可以像添加任何属性一样添加方法:

var A = function() {
    //......
}

var myA = new A();
myA.methodOne = function() { console.log('called methodOne on myA'); }
myA.methodTwo = function() { console.log('called methodTwo on myA'); }

myA.methodOne();
myA.methodTwo();

如果要向实例添加方法,只需添加它们:

a.methodA = function() {
    alert("method A");
};
您可以对任何对象执行此操作。但是,您也可以将它们添加到实例的原型中,这将允许相同的方法在所有其他实例上可见:

var a = new A(),
    b = new A();

a.prototype.methodA = function() {
    alert("method A");
};

b.methodA();
如果要一次性添加多个方法,请创建混合函数或使用框架:

function mix(a, b, typ) {
    var n, o;

    for (n in b) {
        if (! b.hasOwnProperty(n)) continue;

        if (!!(o = b[[n]) && (! typ || typeof o === typ)) {
            a[n] = o;
        }
    }
}
然后

var a = new A();

mix(a, {
    "methodA": function() {
        alert("method A");
    },
    "methodB": function() {
        alert("method B");
    }
}, "function");

如果要向实例添加方法,只需添加它们:

a.methodA = function() {
    alert("method A");
};
您可以对任何对象执行此操作。但是,您也可以将它们添加到实例的原型中,这将允许相同的方法在所有其他实例上可见:

var a = new A(),
    b = new A();

a.prototype.methodA = function() {
    alert("method A");
};

b.methodA();
如果要一次性添加多个方法,请创建混合函数或使用框架:

function mix(a, b, typ) {
    var n, o;

    for (n in b) {
        if (! b.hasOwnProperty(n)) continue;

        if (!!(o = b[[n]) && (! typ || typeof o === typ)) {
            a[n] = o;
        }
    }
}
然后

var a = new A();

mix(a, {
    "methodA": function() {
        alert("method A");
    },
    "methodB": function() {
        alert("method B");
    }
}, "function");
看看你是否同意使用库/框架

A = function(){};
a = new A();
d = {b: function(){ console.log('b') },
     c: function(){ console.log('c') }
    };
$.extend(a, d);
看看你是否同意使用库/框架

A = function(){};
a = new A();
d = {b: function(){ console.log('b') },
     c: function(){ console.log('c') }
    };
$.extend(a, d);

啊哈,谢谢。但如何在一个操作中添加一组方法?为什么要在一个操作中添加它?您的示例是将一个方法添加到对象中,所以如果我已经创建了实例,那么这些方法就不会只是在对象中原型化。但在我的例子中,我不能重新创建对象的实例,所以这个例子对我来说是无用的…你必须在创建第一个InstanceHa之前添加方法,谢谢。但如何在一个操作中添加一组方法?为什么要在一个操作中添加它?您的示例是将一个方法添加到对象中,所以如果我已经创建了实例,那么这些方法就不会只是在对象中原型化。但在我的例子中,我不能重新创建对象的实例,所以这个例子对我来说是无用的…你必须在创建第一个实例之前添加方法