search

Home  >  Q&A  >  body text

javascript - How to understand this onclick statement?

is link[i].onclick = Why are the two brackets ()() used together? How to understand?

As shown below:

Please tell me! Thanks!

高洛峰高洛峰2800 days ago458

reply all(2)I'll reply

  • 滿天的星座

    滿天的星座2017-05-18 10:55:43

    Assumption:

    var test = function(i) {
        return function() {
            alert(i+1);
        }
    }

    So your above line can also be written like this:

    links[i].onclick = test(i);

    The first bracket is to wrap the function name, and the second bracket is to indicate the method parameters

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-18 10:55:43

    Self-executing function, indicating that it directly points to the content of function return.

    reply
    0
  • Cancelreply