How to using ES6 Arrow function to realize Immediately-Invoked Function Expression (IIFE))?

xgqfrms

How to using ES6 Arrow function to realize IIFEImmediately-Invoked Function Expression?

Here is my demo codes, and it had tested passed!

// ES 6 + IIFE
(() => {
    let b = false;
    console.log(`b === ${b}!`);
    const print = `print()`;
    if(window.print){
        b = true;
        console.log(`b === ${b}!`);
    }
    let x = () => {
        if(b){
            console.log(`Your browser support ${print} method.`);
        }else{
            alert(`Your browser does not support ${print} method.`);
            console.log(`Your browser does not support ${print} method.`);
        };
    }
    x();
})();

const dcs = `IIFE: Douglas Crockford's style`;
// ES 5 + IIFE is OK
(function(){
    alert("IIFE: Douglas Crockford's style");
    console.log(dcs + ", ES 5 is OK!");
}());
// Douglas Crockford's style

// ES 6 + IIFE (error)
/*
    (() => {
        alert(`IIFE: Douglas Crockford's style`);
        console.log(`${dcs},ES 6 is Error!`);
    }());
*/
// Douglas Crockford's style
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=3.0">
</head>
<body>
    <main id="print">
        <section>
            <h1>Javascript ES6 & IIEF</h1>
        </section>
    </main>
</body>
</html>

However, there still has something is wrong about Douglas Crockford's style (IIEF)!

screencut enter image description here enter image description here

madox2

Surround it with parentheses:

(() => console.log('hello'))()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Benefit of Immediately-invoked function expression (IIFE) over a normal function

From Dev

Javascript Immediately-invoked function expression (IIFE) and function scoping

From Dev

Javascript Syntax: Immediately Invoked Function Expression (IIFE) with parameters

From Dev

Javascript Syntax: Immediately Invoked Function Expression (IIFE) with parameters

From Dev

HotTowel Angular and Immediately-Invoked Function Expression (IIFE)

From Dev

Javascript closure and IIFE (immediately invoked function expressions)

From Dev

Can anyone explain the output for this code which is based on the concept of IIFE (Immediately Invoked Function Expression)

From Dev

Immediately invoked function expression throws "object is not a function"

From Java

Immediately invoked function expression without using grouping operator

From Dev

Immediately Invoked Function Expression in strict mode

From Dev

Immediately Invoked Function Expression: Where to put the parenthesis?

From Dev

Immediately invoked function expression causing error?

From Dev

Why must I immediately invoke an immediately invoked function expression?

From Dev

Why must I immediately invoke an immediately invoked function expression?

From Dev

Wrapping my gulpfile in an immediately-invoked function expression

From Dev

Prevent Meteor from wrapping scripts in an immediately-invoked function expression

From Dev

Why I need to wrap the code in immediately invoked function expression?

From Dev

Are there any official ways to write an Immediately Invoked Function Expression?

From Dev

Immediately invoked function expression isn't executed on second require

From Dev

How to use `this` in a function called by an ES6 arrow function?

From Dev

ES6 if in arrow function

From Dev

How to get the element from a jQuery on() event using an ES6 arrow function?

From Dev

Immediate function using JavaScript ES6 arrow functions

From Dev

jQuery immediately invoked function in CoffeeScript?

From Dev

rewrite an Immediately-Invoked Function

From Dev

Is there an Immediately Invoked Anonymous Function for Java?

From Dev

jQuery immediately invoked function in CoffeeScript?

From Dev

How to change what an ES6 arrow function's 'this' points to?

From Dev

How do I write an arrow function in ES6 recursively?

Related Related

  1. 1

    Benefit of Immediately-invoked function expression (IIFE) over a normal function

  2. 2

    Javascript Immediately-invoked function expression (IIFE) and function scoping

  3. 3

    Javascript Syntax: Immediately Invoked Function Expression (IIFE) with parameters

  4. 4

    Javascript Syntax: Immediately Invoked Function Expression (IIFE) with parameters

  5. 5

    HotTowel Angular and Immediately-Invoked Function Expression (IIFE)

  6. 6

    Javascript closure and IIFE (immediately invoked function expressions)

  7. 7

    Can anyone explain the output for this code which is based on the concept of IIFE (Immediately Invoked Function Expression)

  8. 8

    Immediately invoked function expression throws "object is not a function"

  9. 9

    Immediately invoked function expression without using grouping operator

  10. 10

    Immediately Invoked Function Expression in strict mode

  11. 11

    Immediately Invoked Function Expression: Where to put the parenthesis?

  12. 12

    Immediately invoked function expression causing error?

  13. 13

    Why must I immediately invoke an immediately invoked function expression?

  14. 14

    Why must I immediately invoke an immediately invoked function expression?

  15. 15

    Wrapping my gulpfile in an immediately-invoked function expression

  16. 16

    Prevent Meteor from wrapping scripts in an immediately-invoked function expression

  17. 17

    Why I need to wrap the code in immediately invoked function expression?

  18. 18

    Are there any official ways to write an Immediately Invoked Function Expression?

  19. 19

    Immediately invoked function expression isn't executed on second require

  20. 20

    How to use `this` in a function called by an ES6 arrow function?

  21. 21

    ES6 if in arrow function

  22. 22

    How to get the element from a jQuery on() event using an ES6 arrow function?

  23. 23

    Immediate function using JavaScript ES6 arrow functions

  24. 24

    jQuery immediately invoked function in CoffeeScript?

  25. 25

    rewrite an Immediately-Invoked Function

  26. 26

    Is there an Immediately Invoked Anonymous Function for Java?

  27. 27

    jQuery immediately invoked function in CoffeeScript?

  28. 28

    How to change what an ES6 arrow function's 'this' points to?

  29. 29

    How do I write an arrow function in ES6 recursively?

HotTag

Archive