最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do we need to wrap ES6 code in an IIFE? - Stack Overflow

programmeradmin3浏览0评论

In ES5, writing such code has been considered as good practice:

(function () {
    //some magic
})();

But in ES6 variables created with let keyword are not attached to window object.

So, is there any need now in writing our code in an IIFE, or it still has some purposes I haven't heard about?

In ES5, writing such code has been considered as good practice:

(function () {
    //some magic
})();

But in ES6 variables created with let keyword are not attached to window object.

So, is there any need now in writing our code in an IIFE, or it still has some purposes I haven't heard about?

Share Improve this question edited May 23, 2016 at 19:53 Michał Perłakowski 92.5k30 gold badges163 silver badges186 bronze badges asked May 23, 2016 at 19:45 Vyacheslav PalamarVyacheslav Palamar 5071 gold badge3 silver badges12 bronze badges 5
  • Related: Namespacing with IIFE in ES6? – Michał Perłakowski Commented May 23, 2016 at 19:56
  • 1 See also Will const and let make the IIFE pattern unnecessary? – Bergi Commented May 23, 2016 at 20:04
  • "variables created with let keyword are not attached to window object" - but they are still global. So if you are writing scripts, you will need to put them in a block or an IIFE. – Bergi Commented May 23, 2016 at 20:05
  • @Bergi Isn't this question a duplicate of the question you linked? – Michał Perłakowski Commented May 23, 2016 at 20:07
  • 1 @Gothdo: Maybe, but I wasn't sure whether the focus on const+let does make them different, so I didn't want to dupe-hammer. Feel free to cast your vote; and if the OP agrees I'll happily close. – Bergi Commented May 23, 2016 at 20:09
Add a comment  | 

2 Answers 2

Reset to default 11

If you're using modules, there's no need to use IIFE (that's how this "wrapper" is called), because all variables have scope limited to the module.

However, there still are some cases when you want to separate one part of the code from another, and then you can use IIFE.

Of course if you're using let or const, you can use a block statement instead of IIFE:

{
  let something = 1;
  const somethingElse = 2;
}
console.log(something); // ReferenceError: something is not defined

See related question on Programmers.SE: How far should encapsulation in JavaScript go?.

It is less of a problem now but I'd argue there's still a reason for that general idea.

Theoretically, it is possible for, say, some third-party library to write code like this following:

let count = 0;
function getCount() {
  return count++;
}

Now, if you tried to create your own count variable in the same scope, you'd get an error:

// 3rd-party
let count = 0;
function getCount() {
  return count++;
}

// Your code
let count = 1;

However, you can make the code cleaner by using actual blocks instead of IIFEs.

// Still bad 3rd party
let count = 0;
function getCount() {
  return count++;
}

// Your code
{
  let count = 10;
  console.log(count);
  console.log(getCount());
  console.log(count);
  console.log(getCount());
}

In the future, you should be able to encapsulate your code in modules which will have their own scope and you won't need to wrap your code in an IIFE or a block.

发布评论

评论列表(0)

  1. 暂无评论