开发者

Returning event handler function

开发者 https://www.devze.com 2023-02-09 00:01 出处:网络
closeButton.onclick = (function(box){ return function(evt){box.display = \'none\';}; })(msgBox); I would like to return a function which will execute on the button click. 开发者_C百科Is this correct
closeButton.onclick = (function(box){
    return function(evt){box.display = 'none';};
})(msgBox);

I would like to return a function which will execute on the button click. 开发者_C百科Is this correct? If not, how it should be written?


Your example works and is using currying, but you could easily do without it. Since msgBox is available in the current scope, you can reference it within the function which will create a closure for msgBox.

closeButton.onclick = function(event) {
    msgBox.display = 'none';
};
0

精彩评论

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