The first one assigns a reference to doSomething
to the onclick
property. The second assigns the return value of doSomething
. That's unlikely to be what you want, unless doSomething
returns a function.
Assign a reference:
function doSomething() {
console.log("something");
}
//When t is clicked, "something" is logged
t.onclick = doSomething;
Assign the return value:
function doSomething() {
console.log("something");
}
//"something" is logged immediately. When clicked, nothing happens
t.onclick = doSomething();
Assign a returned function:
function doSomething() {
return function() {
console.log("something");
};
}
//When t is clicked, "something" is logged
t.onclick = doSomething();