How can I pass arguments to anonymous functions in JavaScript?

前端 未结 10 1412
别那么骄傲
别那么骄傲 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:05

    The delegates:

    function displayMessage(message, f)
    {
        f(message); // execute function "f" with variable "message"
    }
    
    function alerter(message)
    {
        alert(message);
    }
    
    function writer(message)
    {
        document.write(message);
    }
    

    Running the displayMessage function:

    function runDelegate()
    {
        displayMessage("Hello World!", alerter); // alert message
    
        displayMessage("Hello World!", writer); // write message to DOM
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-08 02:08

    What you've done doesn't work because you're binding an event to a function. As such, it's the event which defines the parameters that will be called when the event is raised (i.e. JavaScript doesn't know about your parameter in the function you've bound to onclick so can't pass anything into it).

    You could do this however:

    <input type="button" value="Click me" id="myButton"/>
    
    <script type="text/javascript">
    
        var myButton = document.getElementById("myButton");
    
        var myMessage = "it's working";
    
        var myDelegate = function(message) {
            alert(message);
        }
    
        myButton.onclick = function() { 
            myDelegate(myMessage);
        };
    
    </script>
    
    0 讨论(0)
  • 2020-12-08 02:10

    If you write it like

    myButton.onclick = function() { alert(myMessage); };
    

    It will work, but I don't know if that answers your questions.

    0 讨论(0)
  • 2020-12-08 02:12

    Your specific case can simply be corrected to be working:

    <script type="text/javascript">
      var myButton = document.getElementById("myButton");
      var myMessage = "it's working";
      myButton.onclick = function() { alert(myMessage); };
    </script>
    

    This example will work because the anonymous function created and assigned as a handler to element will have access to variables defined in the context where it was created.

    For the record, a handler (that you assign through setting onxxx property) expects single argument to take that is event object being passed by the DOM, and you cannot force passing other argument in there

    0 讨论(0)
  • 2020-12-08 02:15

    Example:

    <input type="button" value="Click me" id="myButton">
    <script>
        var myButton = document.getElementById("myButton");
        var test = "zipzambam";
        myButton.onclick = function(eventObject) {
            if (!eventObject) {
                eventObject = window.event;
            }
            if (!eventObject.target) {
                eventObject.target = eventObject.srcElement;
            }
            alert(eventObject.target);
            alert(test);
        };
        (function(myMessage) {
            alert(myMessage);
        })("Hello");
    </script>
    
    0 讨论(0)
提交回复
热议问题