Advanced function once

In actual work, we may often encounter certain content that is executed only once and no longer needs to be executed. We can encapsulate these content into functions and use them as parameters of the once function to meet our needs.
Here is the once function:

 function once(fn,context){
    
    
        let res;
        return function(){
    
    
            if(fn){
    
    
                res=context?fn.apply(context,arguments):fn(...arguments)
                fn=null
            }
            return res;
        }
    }

Guess you like

Origin blog.csdn.net/weixin_44494811/article/details/113827111