WSD
WSD

Reputation: 3587

Adding Methods on the Fly to Object

I hope this can be done in Javascript.

I Have something like this:

have something like this:

var MYOBJECT = {
method1: function(){//some cool},
addMethodToObject: function (a_new_method){
//whats need to be here to add the
 method passed in "a_new_method" to MYOBJECT ?
}

}

The idea is to call:

MYOBJECT.addMethodToObject(function otherMethod(needThis){return needThis;});

and now MYOBJECT has the Method passed as a new Method, and I can do something like this:

MYOBJECT.otherMethod('say Hi');

will output

"say Hi"

Thanks in advance.

Upvotes: 0

Views: 751

Answers (5)

Romms
Romms

Reputation: 29

I have not tested the answers above but all of them need to change the object from inside. I needed something without changing the object from the inside and so I came up with this solution:

First create and instance of your object, then:

yourObject.yourMethodName = function(methodParam1, methodParam2) {
    doWhatYouNeed;
};

Then execute as:

yourObject.yourMethodName(methodParam1, methodParam2);

Hope this helps.

Upvotes: 3

Tuan
Tuan

Reputation: 5412

Pretty much like others have stated, just pass in an anonymous function as an argument to your addMethodToObject function.

var MYOBJECT = {
    method1: function(){ ; },
    addMethodToObject: function (methodName, method) {
        this[methodName] = method;
    }
};

MYOBJECT.addMethodToObject("newMethod", function () {
    console.log("testing 123");
});

MYOBJECT.newMethod();

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1074148

You can define a function to do that. You don't need to, though, just assign to MYOBJECT directly:

MYOBJECT.otherMethod = otherMethod;
function otherMethod(needThis){
    return needThis;
}

Functions are first-class objects in JavaScript, you can pass around references to them just like any other object. And you can create new properties on a JavaScript object at any time, just by assigning a value to the property.

If you want to define a function for it, the function will have to accept both the function and the name you want it to have on the object, like this, which looks like this:

var MYOBJECT = {
    // ...
    addMethodToObject: function(name, func) {
        this[name] = func;
    }
};

That's because there's no reason the property and the function have to have the same name, and there's no standard way to get the name of the function from the function instance. (There are a couple of non-standard ways, but nothing defined by the standard.)

The syntax I used there may be unfamiliar. In JavaScript, there are two ways to access properties: Dotted notation with a literal name, and bracketed notation with a string. Examples are the clearest way to demonstrate it:

var obj = {};
obj.foo = 42;    // Dotted notation with a literal name `foo`
obj["foo"] = 42; // Exactly the same thing, using bracketed notation and a string
var s = "foo";
obj[s] = 42;     // Again the same, because the string doesn't have to be a literal

...which is why this[name] = func; works; name is the string name of the property to add.

Upvotes: 1

Parv Sharma
Parv Sharma

Reputation: 12705

simple u can just add a new method like u add a new property

obj["MethodName"] = function(){//ur Javascript statements}

Upvotes: 1

davidbuzatto
davidbuzatto

Reputation: 9424

Try this:

function test() {

  myObj = {
    addMethod: function( method, name ) {
      this[name] = method;
    }
  }

  myObj.addMethod( function() { alert( "foo" ) }, "fooMethod" );
  myObj.fooMethod();

}

See ya ;)

Upvotes: 1

Related Questions