How can I pass arguments to anonymous functions in JavaScript?

前端 未结 10 1454
别那么骄傲
别那么骄傲 2020-12-08 01:37

I\'m trying to figure out how to pass arguments to an anonymous function in JavaScript.

Check out this sample code and I think you will see what I mean:



        
10条回答
  •  清歌不尽
    2020-12-08 02:06

    Event handlers expect one parameter which is the event that was fired. You happen to be renaming that to 'myMessage' and therefore you are alerting the event object rather than your message.

    A closure can allow you to reference the variable you have defined outside the function however if you are using Jquery you may want to look at its event specific API e.g.

    http://docs.jquery.com/Events/bind#typedatafn

    This has an option for passing in your own data.

提交回复
热议问题