what is the difference between calling function in JavaScript with or without parentheses ()

前端 未结 3 1847
误落风尘
误落风尘 2020-12-04 02:20

so a simple example would be

function a() {
    alert(\"something\");
}

anything.onclick = a; // this is without parentheses

anything.onclick = a(); // th         


        
3条回答
  •  无人及你
    2020-12-04 02:44

    The parenthesis at the end of the function is the permission for js engine to execute the function. If you don't supply it, it won't be executed at all.

    x=a() //if you do this you are invoking the function but if u just pass "a" you are passing a pointer to a function

提交回复
热议问题