How to using ES6 Arrow function to realize Immediately-Invoked Function Expression (IIFE))? [closed]
Asked Answered
N

1

-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

Natividad answered 6/11, 2016 at 10:24 Comment(12)
In your case, why do you want to execute them right now?Svensen
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.Pinkham
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 ()Mcduffie
If you meant () => {...} instead of function() {...}, then it's "Arrow function".Mcduffie
And an IIFE is (function(){})()Grapevine
Althought, I typed error words you should easily saw that what I mean !Natividad
As a developer of web, if you heard of ES6, you should know Arrow Function !== Array function, isn't it!Natividad
@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.Mcduffie
OK, if you don't want to answer this question, you can just let it go! Do not make some thing meanless comments!Natividad
"Immediately-invoked function expression" as a term describes a design pattern which has also been referred to as a "self-executing anonymous function."Natividad
en.wikipedia.org/wiki/…Natividad
Duplicate of #22139050Aiaia
H
1

Surround it with parentheses:

(() => console.log('hello'))()
Hyperion answered 6/11, 2016 at 10:27 Comment(1)
Thanks for your time, it seems works!Natividad

© 2022 - 2024 — McMap. All rights reserved.