Function pointers in objects

I have a javascript function that takes an object as a parameter. I want one of the properties of the passed object to be a pointer to a function. But the function is executed, and I do not want to do this.

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 am sure that there is an expert who can lead me in the right direction. I'm going about it wrong. A quick fix is ​​preferable if it is.

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 */ }); } 
+4
source share
3 answers

I want one of the properties of the passed object to be a pointer to a function. But the function is executed, and I do not want to do this.

This is similar to the fact that instead of passing a reference to a function, you execute the function and pass the return value.

Maybe your source code has

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

which calls myFuncPtr and sets the return value to onDone .

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


As already mentioned, there are no pointers in JavaScript, it is much simpler. Functions are only data types, such as strings, numbers, arrays, etc. that can be called.

+2
source
 var myFuncPtr = function(){alert("Done");}; function myFunc(o) { alert("Hello " + o.foo); o.onDone(); } myFunc( {foo:"bar", onDone: myFuncPtr} ); 

This is your trick. The onDone property of your object "points" to the correct function, you can just call it.

0
source

Theerrrreee is not a pointer to ECMAscript ...... Asposx.yxm - dfgdfg.: !!

next to this ... you can just call:

 o.onDone(); 

inside myFunc() .


PS:
There is only reference this language.

-one
source

Source: https://habr.com/ru/post/1393985/


All Articles