-2

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

xgqfrms
  • 10,077
  • 1
  • 69
  • 68
  • 3
    In your case, why do you want to execute them right now? – gurvinder372 Nov 06 '16 at 10:29
  • 3
    Your question is unclear. Please have a friend or colleague help you with your English. Perfect English is **not** required at all, but we do need to be able to understand what you're asking. Right now, it seems like your question and your code are completely unrelated to each other. – T.J. Crowder Nov 06 '16 at 10:29
  • 2
    Not sure what you mean by "Array function" but right now your outer function is executed immediately instead of being assigned to `window.onload` because of the `()` at the very end. If you don't want that, then remove the `()` –  Nov 06 '16 at 10:29
  • If you meant `() => {...}` instead of `function() {...}`, then it's "Arrow function". –  Nov 06 '16 at 10:35
  • And an IIFE is `(function(){})()` – mplungjan Nov 06 '16 at 10:46
  • Althought, I typed error words you should easily saw that what I mean ! – xgqfrms Nov 06 '16 at 10:50
  • As a developer of web, if you heard of ES6, you should know Arrow Function !== Array function, isn't it! – xgqfrms Nov 06 '16 at 10:53
  • @xgqfrms: The problem was that with the wording, your question was unclear, so it makes it more difficult to know exactly what you meant. What also added to the confusion was that your immediately invoked function result is being assigned to `window.onload`, but there's no return value. And yes, *Arrow Function !== Array function*, so we only wanted to be sure we understood correctly. No need to get upset when people ask for clarification. –  Nov 06 '16 at 10:55
  • OK, if you don't want to answer this question, you can just let it go! Do not make some thing meanless comments! – xgqfrms Nov 06 '16 at 11:11
  • "**Immediately-invoked function expression**" as a term describes a design pattern which has also been referred to as a "**self-executing anonymous function**." – xgqfrms Dec 02 '16 at 11:28
  • https://en.wikipedia.org/wiki/Immediately-invoked_function_expression#Terminology – xgqfrms Dec 02 '16 at 11:29
  • Duplicate of https://stackoverflow.com/questions/22138550/immediate-function-using-javascript-es6-arrow-functions?rq=1 – Bergi Aug 07 '17 at 22:01

1 Answers1

1

Surround it with parentheses:

(() => console.log('hello'))()
madox2
  • 49,493
  • 17
  • 99
  • 99