-1
if ((() => {
    let hasChrome = false;
    let hasBrowser = false;
    try {
        hasChrome = (typeof chrome === 'object' && chrome !== null && typeof chrome.runtime !== 'undefined');
    } catch (e) {
        // NOP
    }
    try {
        hasBrowser = (typeof browser === 'object' && browser !== null && typeof browser.runtime !== 'undefined');
    } catch (e) {
        // NOP
    }
    return (hasBrowser && !hasChrome);
})()) {
    chrome = browser;
}

The code above seems an arrow function,but I wonder where is the name of the function? How to understand it?
Shouldn't arrow function like this?
hello = () => { return "Hello World!"; }

Any assistance is highly appreciated and thanks in advance.

Ye Shiwei
  • 9
  • 5

3 Answers3

2

Arrow functions are also anonymous when you use them in the way it shows in the snippet. Also, we use something called an Immediately Invoked Function Expression (IIFE) such that the return value of the arrow function is passed as a condition.

1

Generally speaking,

() => { return "Hello World!"; }

this is the arrow function

hello = () => { return "Hello World!"; }

here the arrow function is assigned to variable "hello" just so that it can be referenced like hello().

Arrow functions don't necessarily have to be assigned and can be called anonymously as well.

eg:

promise.then(()=>{console.log('hello world')});

here the arrow function is executed immediately after the promise.

Ron
  • 99
  • 2
0

The function doesn't have a name. It's anonymous and it executes immediately. If you look closely, you can see that the entire function is wrapped in parantheses
This

(
  ()=>{
   // do stuff
   return null
  }
)()

is the same as this

(
  function(){
    //do stuff
    return null
   }
)()
PhantomSpooks
  • 2,877
  • 2
  • 8
  • 13