The native JavaScript binding onclick event produces the effect of onload?

the event onclick is bound in the way of native JavaScript object binding, but the actual effect is similar to that of onload .
wants a mouse click button pop-up prompt, but it actually pops up as soon as is loaded on the page. click has no effect .

to do this, I tried three ways:

    The
  1. real name function is called in the button property ( property binding ). OK!
  2. Anonymous function, property binding and object binding are OK!
  3. The error mentioned above occurs when
  4. adapts the function and assigns a value to the button object"s property onclick ( object binding ). Error

Please see the code film:
1. Object binding event ( failed )

////
<button id="b"  ></button>
<script type="text/javascript">
//fun(), //
function fun() {
alert("xxxx");
}

document.getElementById("b").onclick = fun();  //?: onclick onload?

2. Anonymous function binding ( successful )

////
<button id="b"  ></button>
<script type="text/javascript">
//fun(), //
document.getElementById("b").onclick = function(){
   alert("xxxx");
}

3. Real name function, attribute binding ( successful )

//buttononclickfun()//
<button id="b"  onclick="fun()"></button>
<script type="text/javascript">
////
function fun() {
    alert("xxxx");
}

Why does the first approach fail? What is the mechanism that causes this?

Mar.15,2021

I don't think you understand it correctly in some aspects. In javascript, functions are first-class citizens, so you can assign values and other operations.

but fun () with parentheses is the execution of the function, so first execute the fun function, and then return the value assigned to onclick

.

xxx.onclick = fun the function fun is assigned to onclick , and the function is not executed.

you can check fun and fun () respectively, one is function, and the other is the returned value of the result

  

there are other events as well

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b38522-2c0fe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b38522-2c0fe.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?