Practical Application of Corey (curring)

topic description

In the original

, the following related code partially withdraws the early return and delayed execution of the Corialization function, and the in the code only needs to determine the browser type once. My understanding of the code is that every time addEvent is actually called, the code executes a function, and then if (window.addEventListener) {.} makes a browser judgment.
feels that my idea is at odds with the author"s, so I want to ask why if (window.addEventListener) is only executed once in practice. Even if addEvent is called next time, browser judgment will not be made

sources of topics and their own ideas

this is a case I saw on the Nuggets, with the code at the end

author: Xiaoxing nice
link:


defines the addEvent and executes it immediately, and after executing it once, the execution function of addEvent is determined. You are misled by the returned execution function. After addEvent initialization, it has nothing to do with the original function body. You can change the way of thinking and change the return function of addEvent to return an object, which makes it easier to understand

.
var obj = (function(){
      console.log('init')
      if(window.addEventListener){
        return {
          func:window.addEventListener
        }
      } else {
        return {
          func:window.attachEvent
        }
      }
    })()

    console.log(obj.func)
    console.log(obj.func)
    console.log(obj.func)

isn't this the immediate execution of the function? It will only be run once at the beginning, and then addEvent saves the result of its run, that is, the returned function

.
Menu