Jump to content

Dynamically add functions to a class


mikesta707

Recommended Posts

Is there a way to dynamically add a function to an already existing class? What I want to do is create a function that will allow the user to define new functions that are not already in the class. I have the following so far which doesn't produce any errors, but doesn't work even in the slightest

 

function setCustomCriteria(func, name, where){
eval(func);
eval("this."+name+"="+name);
eval("this."+name+"()");
}

 

and here is where I call it

 

function go(){ alert("Worked!"); }

object.setCustomCriteria(go, "go", "nowhere");

 

I'm sure that is horribly wrong, but that was just a shot in the dark because, frankly, im sort of lost

Link to comment
Share on other sites

Nevermind, figured it out. This is awesome!

 

function setCustomFunc(name){
       //The line below will add the function that whatever class this function is part of
eval("this."+name+"="+name);
        // the line below will execute the function
//eval("this."+name+"()");


}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.