In fact, it is summarized as follows:
As long as the function is to be called for execution, it must be parenthesed. At this time, function() is actually equal to the return value of the function. Of course, some have no return value, but have already performed the behavior in the function body. This is fundamental, that is, as long as brackets are added, it means that the function body code will be executed.
Those without brackets are used to pass parameters using the function name as a pointer to the function. At this time, the result of the function is not obtained because the function body code will not be run. It just passes the address location of the function body, so that it can find the function body to execute when needed.
So usually we use the reason for no brackets. This is also due to the ambiguous nature of brackets. Because brackets are "function call operators", which is equivalent to executing such a function, the problems arising will be understood after understanding them.
In addition: In addition to not adding brackets on both sides, you can also add brackets on both sides to realize the copy of the function, rather than executing the function. If there are no brackets on the left and there are brackets on the right, it is actually equivalent to producing a property rather than a method. When calling, only the attribute name or function name is used, and there is no need to use the function call operator parentheses, because it is equivalent to a property. Of course, it can also be called in the form of function calls, and parentheses are added. Take the example above, if written as person.sayHello =sayHi(), you can directly use person.sayHello or person.sayHello() when calling sayHello. Both achieve the same effect.
Baidu knows the answer:
As long as the function is to be called for execution, it must be parenthesed. At this time, function() is actually equal to the return value of the function. Of course, some have no return value, but have already performed the behavior in the function body. This is fundamental, that is, as long as brackets are added, it means that the function body code will be executed.
Those without brackets are used to pass parameters using the function name as a pointer to the function. At this time, the result of the function is not obtained because the function body code will not be run. It just passes the address location of the function body, so that it can find the function body to execute when needed.
The above article briefly discusses the issue of adding brackets when calling functions in js. This is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.