开发者

jQuery , bind a callback to any function

开发者 https://www.devze.com 2023-02-25 07:17 出处:网络
I would like to be able to bind a callback function to a previously defined function and being able to use two variables defined in the scope of that function.

I would like to be able to bind a callback function to a previously defined function and being able to use two variables defined in the scope of that function.

It should look like this:

function mainFunction(){开发者_如何学运维
  var localForMain;
}

 $(document).ready(function(){

     // bind a call back method to the 'mainFunction'
     $(mainFunction).bindCallback(function(){

        // ...use the localForMain variable HERE;

     });


 });

Is it possible to achieve something like this?


it is not possible to bind call back function in such way. You have to program it inside the function.

function SampleWithCallBack(callbackfunction)
{
      code here
      callbackfunction(params);
}

about the use of defined variable in the context. You should pass the variables in parameters or define it globally which is not a very good way


to achieve this you need to be in scope of mainFunction. It could be done fairly easy:

function mainFunction(){
  var localForMain;


 $(document).ready(function(){

     // bind a call back method to the 'mainFunction'
     $(mainFunction).bindCallback(function(){

        // NOW YOU CAN USE the localForMain variable HERE;

     });


 });

}

0

精彩评论

暂无评论...
验证码 换一张
取 消