0

Well, i understand my question is a little bit strange and the answer seems obvious ("it's impossible !"), but, as JS is a very open language, i go on anyway :)

Let's say, we have the following code :

function dummy() {
}
var obj = new dummy();
var result = obj.aFunction('a','b');

Obviously, the JS interpretor says :

obj.aFunction is not a function

I understand his anger :) but is there a trick to bypass the interpretor (or something like that) or to create on-the-fly the aFunction function into the obj object before the interpretor evaluates all the stuff ?

I've red all about using dynamic function names and so on (using eval() and other tricks) but that don't solve my (weird) problem ...

Thanks in advance.

EDIT : Well, folks, thanks for your answers, but it's not my problematic.

In fact, i used to code in Java with AOP and what i want is :

  • Create a Valve (or something like this) that catches all the exceptions
  • Analyse the exception
  • if the exception corresponds to my 'no function' error, i create from scratch the function and execute it
  • I garbage this exception
  • if it's not the good exception i let it to continue its job

Unfortunatly, You cannot do that in JS. Using an ExceptionHandler is not sufficient because its API is too poor ...

But, Thanks to all ...

jcvidal
  • 11
  • 1

2 Answers2

0

There are many ways to achieve this. Here is one:

function dummy() {
}
dummy.prototype.aFunction = function (a, b) {
  alert(a + ', ' + b);
};
var obj = new dummy();
var result = obj.aFunction('a','b');

Here is a working example. Warning: it will pop up an alert box.

http://jsfiddle.net/X4F67/

Here is another, if you don't know the function name at runtime:

var name = 'aFunction';
function dummy() {
}
dummy.prototype[name] = function (a, b) {
  alert(a + ', ' + b);
};
var obj = new dummy();
var result = obj[name]('a','b');

Fiddle: http://jsfiddle.net/X4F67/1/

Note that both of these affect the dummy prototype itself, so all instances of dummy would contain aFunction. If you instead want to work directly on the obj, you could use something like this:

...
var obj = new dummy();
obj[name] = function () { };
Brandon Gano
  • 6,430
  • 1
  • 25
  • 25
0

You can create a function on the fly, your code just has some issues.

First of all, setting:

var result = obj.aFunction('a','b');

Is making the browser think that obj.aFunction is an existing function that you are calling, you have to set the function.

obj.aFunction = function(a,b)
{
   alert(a + ' ' + b);
}.bind(obj);

...then you could state:

var result = obj.aFunction;

If you were to say:

var result = obj.aFunction('a', 'b');

...after declaring obj.aFunction, it would be called at that line and alert right away.

Instead, you can do this:

window['result']('a', 'b');

As for declaring a function on the fly, such as writing out a function as a string and using eval() to evaluate the function and append it to a variable, I'm not sure why it doesnt work. This is what I tried:

obj.aFunction = eval("function(a,b){alert(a + ' ' + b);}.bind(obj)");

And I get:

Uncaught Syntax Error: Unexpected token (

Either way, hope that helps.

WebWanderer
  • 10,380
  • 3
  • 32
  • 51