开发者

javascript function - return value chain

开发者 https://www.devze.com 2023-01-23 17:16 出处:网络
I need to access local variable returned by function inside a chained function ex. $(\"#history_table\").bind(\"sortStart\", function() {

I need to access local variable returned by function inside a chained function

ex.

$("#history_table").bind("sortStart", function() {
     var a=30;
     return a;
}).bind("sortEnd", function() {
     alert(a);               
});

here in this 开发者_如何学Goexample I need to access variable a returned by the first function, sortStart and aortEnd events will trigger the two functions asynchronously...


The variable needs to be declared outside:

var a = 0;
$("#history_table").bind("sortStart", function() {
     a=30;
     return a;
}).bind("sortEnd", function() {
     alert(a);               
});

or make it as property of the current object using the .data() function:

$("#history_table").bind("sortStart", function() {
     var a = 30;
     $(this).data('a', a);
     return a;
}).bind("sortEnd", function() {
     var a = $(this).data('a');
     alert(a);               
});
0

精彩评论

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