Skip to content Skip to sidebar Skip to footer

Function Pointers In Objects

I have a javascript function that takes an object as a parameter. I want one of the properties of passed object to be a function pointer. But the function is executed which I don

Solution 1:

I want one of the properties of passed object to be a function pointer. But the function is executed which I don't want to happen.

That sounds like instead of passing a reference to the function, you are executing the function and passing the return value.

Maybe in your original code you have

myFunc( {foo:"bar", onDone: myFuncPtr()} );

which calls myFuncPtr and assigns the return value to onDone.

The code in the question is fine though, just call o.onDone() inside myFunc.


As already said, there are no pointers in JavaScript, it's much simpler than that. Functions are just data types such as strings, numbers, arrays etc, which happen to be callable.


Solution 2:

var myFuncPtr = function(){alert("Done");};

function myFunc(o)
{
    alert("Hello " + o.foo);
    o.onDone();
}
myFunc( {foo:"bar", onDone: myFuncPtr} );

This does your trick. The onDone property of your object 'points' to the correct function, you can just call that.


Solution 3:

Theerrrreee is no pointer in ECMAscript...... Asposx.yxm--dfgdfg.:!!

beside that... you might just want to call:

o.onDone();

within myFunc().



Post a Comment for "Function Pointers In Objects"