4

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't want to happen.

Example:

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

function myFunc(o){
  // do something with passed object
  // then call the function pointed at in the passed object
}

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

I'm sure there's an expert out there who can guide me in the right direction. Am I going about this the wrong way. The quick solution is preferred if there is one.

Here is the actual code:

$(document).ready(function() {
    imageCropper({
        id:"imageBox",
        editWidth:400,
        cropWidth:0,
        maxFileSize:3000000,
        croppedId:"croppedBox",
        onCrop:whenImageCropped
    });
});

function whenImageCropped(o)
{
    alert("done");
}

function imageCropper(options)
{
    divId = options.id;
    croppedDivId = (options.croppedId)?options.croppedId:divId+"_croppedPic";

    $("#"+divId).load("/modules/imageCropper.asp", options, function(){

/* if i remove this bit, then no probs */
        $("#"+divId+"_imgToCrop").Jcrop({
            },function(){jcrop_api = this;}
        );
/* end of - if i remove this bit, then no probs */

    });
}
1
  • 1
    May I ask what this "function pointer" will be used for or how it is used? Commented Jan 31, 2012 at 13:56

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Some more information: the sample code I submitted above works as expected. I have discovered that it is something in the function "pointed" at by myFuncPtr. There is a jquery load used in it which in turn executes an anonymous callback function. If I empty this callback function, then no problem. This callback function is using Jcrop. Is there any known issues with Jcrop used this way?
I cannot say. It could always be that some code throws an error which then prevents the execution of the rest of the code. Or you might run into issues with asynchronous code execution. But that all depends on your actual code and what you are doing and cannot be answered generally.
I have edited my original question and now included the actual code
And what is the problem? Do you get any error? Otherwise it is difficult to help. From what I can see is that saveCoords and options.ratio are not defined and you are actually never calling options.onCrop.
The whenImageCropped function is executed as opposed to just passed as a "pointer" as one of the properties in the object passed in the imageCropper call. Correct I'm not calling whenImageCropped (yet). saveCoords just populates some input boxes in the document.
|
0
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.

Comments

-1

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

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

o.onDone();

within myFunc().


PS:
There is only reference in this language.

1 Comment

There are indeed no native pointers as we are used to in for example C++, but it sure is possible what the op is trying to do. Also; WTF!?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.