Monday, January 2, 2023

Typescript code through self calling function ()() Eamples

 (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:

(function (arg1: number, arg2: string) {
  console.log(`I'm a self-calling function with arguments: ${arg1}, ${arg2}`);
})(42, "hello");

This will print the message "I'm a self-calling function with arguments: 42, hello" to the console.

No comments:

Post a Comment

How AI (Artifical Inteligence) is Revolutionizing Grief Support: The Story of Digital Legacies and Memory Preservation

When James Vlahos learned his father was diagnosed with terminal cancer in 2016, he was heartbroken. Living in Oakland, California, James ch...