search

Home  >  Q&A  >  body text

javascript - For example, the function name is f1, the difference between f1 and f1()

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>    
    <h2>DOM 2级事件绑定</h2>
    
    <button>解绑</button>

    <script type="text/javascript">
    
        var h2 = document.getElementsByTagName('h2')[0];
        function f1(){
            console.log('甲程序员的小功能');
        }
        //甲程序员
        h2.addEventListener('click',f1);

        //解绑
        var btn = document.getElementsByTagName('button')[0];
        btn.onclick = function(){
            h2.removeEventListener('click',f1);
        }
        
        //乙程序员
        h2.addEventListener('click',function(){
            console.log('乙程序员的小功能');
        });

        //丙程序员
        h2.addEventListener('click',function(){
            console.log('丙程序员的小功能');
        });

        
    
    </script>
</body>
</html>
仅有的幸福仅有的幸福2716 days ago775

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-06-26 10:58:29

    f1 is the function object itself. The function is equivalent to assigning the function object to the variable of the function name, so you can access the function just like accessing the variable.
    f1() is the calling function.

    function test() { console.log('hello'); }
    function callfun(f) { f(); } // Pass function `f` and call it in the function.
    callfun(test);   // Call `test` and print 'hello'

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-06-26 10:58:29

    f1() gets the return value of the function call, and f1 represents the pointer to a function

    reply
    0
  • Cancelreply