# Comparing a closure with and without IIFE

**URL:** <https://forum.kirupa.com/t/comparing-a-closure-with-and-without-iife/638327>\
**Category:** programming\
**Created:** [June 4, 2018, 8:52pm UTC](https://forum.kirupa.com/t/comparing-a-closure-with-and-without-iife/638327 "2018-06-04T20:52:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kayut](https://avatars.discourse-cdn.com/v4/letter/k/bcef8e/32.png) [@kayut](https://forum.kirupa.com/u/kayut)\
**Post date:** [June 4, 2018, 8:52pm UTC](https://forum.kirupa.com/t/comparing-a-closure-with-and-without-iife/638327/1 "2018-06-04T20:52:46Z")

</div>

Hey,

If I use a closure inside an IIFE like this:

```auto
var outerFunction = (function() { // this is an IIFE
  const outer = `I'm the outer function!`;

  var innerFunction = function() {
    const inner = `I'm the inner function!`;
    console.log(outer); // I'm a variable from the outer function!
  }

  return {
	kir:innerFunction
	} 
})();
```

It seems to me that the IIFE returns an object called outerFunction and add the method kir to it.  
So that now, I can access the kir method of the object outerFunction with the following code:

```auto
outerFunction.kir();
```

But if I don’t use an IIFE for the same example:

```auto
function outerFunction () {
  const outer = `I'm the outer function!`;

  var innerFunction = function() { 
    const inner = `I'm the inner function!`;
    console.log(outer); // I'm the variable from the outer function!
  }

  return {			
	kir:innerFunction
	} 
}
```

I can access the method kir only if I run this:

```auto
outerFunction().kir();
```

Why it doesn’t work if I run this?:

```auto
outerFunction();
outerFunction.kir();
```

It seems to me that in the first example, when the IIFE runs automatically, it returns the object outerFunction and add the method kir to it.  
But when I run outerFunction manually, it’s different and I can’t access the method kir.

I’m a bit confused!  
Can some one please explain to me why I can’t access kir method in the second example by running this?:

```auto
outerFunction();
outerFunction.kir();
```

I guess this is more a question about understanding IIFE rather than the closure.

Thanks

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [June 4, 2018, 9:38pm UTC](https://forum.kirupa.com/t/comparing-a-closure-with-and-without-iife/638327/2 "2018-06-04T21:38:23Z")

</div>

Part of your problem is your naming. In your IIFE, `outerFunction` is _not_ a function. Its the return value of the outer function. A more accurate version would be:

```auto
var object = (function outerFunction() { // this is an IIFE
  const outer = `I'm the outer function!`;

  var innerFunction = function() {
    const inner = `I'm the inner function!`;
    console.log(outer); // I'm a variable from the outer function!
  }

  return {
	kir:innerFunction
	} 
})();

```

The value in `object` is `{ kir:innerFunction }`.

With your second example, `outerFunction` is the actual function. Running that function doesn’t modify the function, rather (as with the first example) returns an object with the `kir` property.

```auto
var object = outerFunction(); // returns { kir:innerFunction }
object.kir();

```

The difference is that with the second example, you can call `outerFunction` multiple times whereas with the IIFE, it happens only once.
