Javascript: ¿mejor forma de agregar métodos dinámicos?
Me pregunto si hay una mejor manera de agregar métodos dinámicos a un objeto existente. Básicamente, estoy tratando de ensamblar nuevos métodos dinámicamente y luego agregarlos a una función existente.
Este código de demostración funciona.
builder = function(fn, methods){
//method builder
for(p in methods){
method = 'fn.' + p + '=' + methods[p];
eval(method);
}
return fn;
}
test = {}
test = builder(test, {'one':'function(){ alert("one"); }','two':'function(){ alert("two"); }'} );
test.one();
test.two();