-2

IIFE를 실현하기 위해 ES6 화살표 기능을 사용하는 방법 Immediately-Invoked Function Expression?ES6 Arrow 기능을 사용하여 즉시 호출 함수 식 (IIFE)을 구현하는 방법)

여기 내 데모 코드이며 테스트를 통과했습니다!

// 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>

그러나, 여전히 뭔가가있다 더글러스 크록 포드의 스타일 (IIEF)에 대해 잘못입니다!

screencut enter image description here enter image description here

+3

을, 왜 지금을 실행 하시겠습니까? – gurvinder372

+3

질문이 명확하지 않습니다. 친구 나 동료가 영어로 도움을 받도록하십시오. 완벽한 영어는 ** 전혀 필요하지 않습니다 **하지만 우리는 당신이 무엇을 요구하는지 이해할 수 있어야합니다. 지금 당장은 귀하의 질문과 같고 귀하의 코드는 서로 관련이 없습니다. –

+2

"Array function"이 무슨 의미인지 모르겠지만, 바로 바깥 쪽 함수는 맨 마지막에'() '때문에'window.onload'에 할당되는 대신 즉시 실행됩니다. 원하지 않으면'()'을 제거하십시오. –

답변

1

괄호로 서라운드 : 귀하의 경우에는

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

시간 내 주셔서 감사합니다. – xgqfrms