(function () {
console.log("I'm a self-calling function!");
})();
This function is defined using an anonymous function
expression, which is immediately followed by () to call the function.
When the code is executed, the function is called immediately, and the message
"I'm a self-calling function!" is printed to the console.
You can also define the self-calling function using a named
function expression, like this:
(function myFunction() {
console.log("I'm a self-calling function!");
})();
In this case, the function is given the name myFunction, which can be useful for debugging or for adding documentation. However, the function cannot be accessed from outside the self-calling function.
You can also pass arguments to the self-calling function,
like this:
This will print the message "I'm a self-calling function with arguments: 42, hello" to the console.
No comments:
Post a Comment