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

Javascript why wrap a variable or constructor in an IIFE? - Stack Overflow

programmeradmin0浏览0评论

I saw something like this today

var Visualizer = (function() {
    function Visualizer() {
    //...
    }
    Visualizer.prototype.function1 = function () { /* ... */ }
    //...
    return Visualizer;
})();

var viz = new Visualizer();

I don't understand the point of this versus just getting rid of the iife wrapper.

I saw something like this today

var Visualizer = (function() {
    function Visualizer() {
    //...
    }
    Visualizer.prototype.function1 = function () { /* ... */ }
    //...
    return Visualizer;
})();

var viz = new Visualizer();

I don't understand the point of this versus just getting rid of the iife wrapper.

Share Improve this question edited May 22, 2019 at 6:26 Jan 2,2493 gold badges18 silver badges30 bronze badges asked Mar 28, 2014 at 5:36 natecraft1natecraft1 2,8369 gold badges41 silver badges58 bronze badges 4
  • 1 Well, in that specific case there isn't much benefit, but what if you need some helper functions? That's when it's useful because you keep them private, and don't leak to the global scope. – elclanrs Commented Mar 28, 2014 at 5:37
  • as in putting the helper functions inside the wrapper but not the function? what's the difference between just putting them in the function? – natecraft1 Commented Mar 28, 2014 at 5:39
  • 1 If you do that you'd be creating the helper functions each time you call new. – elclanrs Commented Mar 28, 2014 at 5:40
  • 1 It allows to create scoped variables, some variables may only available in the function but not in the global scope. – The Alpha Commented Mar 28, 2014 at 5:44
Add a comment  | 

2 Answers 2

Reset to default 23

There's no point for the specific construct that you show here. The reason to use an IIFE in this type of construct is when you have static data that you need to declare, want to be available to your object, but don't want it to be publicly accessible or interfere with the global namespace or be instance data.

Since the code you show doesn't show any of those, it isn't really offering any benefit as you've shown. But, if there were some other variables declared outside the object, but inside the IIFE, then the IIFE would protect and enclose them and isolate them from the outside world.

For example, if you had this:

Visualizer = (function() {
  var counter = 0;
  function Visualizer() {
    counter++;
    ...
  }
  Visualizer.prototype.getCount = function () { return counter; }
  ...
  return Visualizer;
})();

var viz = new Visualizer();

Then, the IIFE would be enclosing a variable counter that would be available to all methods of all instances of Visualizer, but isolated from the outside world and the IIFE would be offering some potential benefit.

Sorry I worded vaguely, but I think JS with saying words need to be bracket properly to know what operation takes priority and what belongs to what is equivalent to simply saying "know what is in what" and stating python was just saying that if it did not have these brackets it would properly resort to method like python where there is no need for brackets and ; where indentation is important. Possibly I've miss the whole question but I think I got it about right.

发布评论

评论列表(0)

  1. 暂无评论