开发者

javascript function - return value chain

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜