0

This is a piece of code I am running.

var passed = 3;

var addTo = function(){


    return ++passed;

}
console.dir(addTo);

If I run it from JS fiddle: I get closure in [[Scopes]], in chrome console:enter image description here

But If I run entire code in chrome console, I am not getting closure in chrome console.enter image description here

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
dasfdsa
  • 7,102
  • 8
  • 51
  • 93
  • 2
    That's why the console is used for **debugging**: because it gives a lot of information. – ibrahim mahrir Nov 28 '17 at 14:39
  • 4
    js fiddle wraps the code in a window.onload callback. – synthet1c Nov 28 '17 at 14:39
  • 3
    Right, jsfiddle by default wraps your code in `window.onload` - that's what it's telling you, you can change that in the options - [updated fiddle](https://jsfiddle.net/mzLvk57p/1/). – James Thorpe Nov 28 '17 at 14:39
  • 1
    @synthet1c It has nothing to do with jsFiddle. – ibrahim mahrir Nov 28 '17 at 14:40
  • @ibrahimmahrir um.. yeah it does, js fiddle is decorating the console.log function within the scope of the onload callback. – synthet1c Nov 28 '17 at 14:41
  • @ibrahimmahrir It has everything to do with jsfiddle, and its default option. See my link to an updated fiddle with the option changed. – James Thorpe Nov 28 '17 at 14:41
  • @synthet1c Open your console > type the code above > unfold the log > wow! there it is `[[scopes]]`. So where is jsFiddle in all of this? – ibrahim mahrir Nov 28 '17 at 14:42
  • you asked for it. – synthet1c Nov 28 '17 at 14:42
  • 2
    @ibrahimmahrir When you run it in the console, you get `[[Scopes]]` with a single entry. When you run it in jsfiddle with the default options, you get it with two entries. – James Thorpe Nov 28 '17 at 14:43
  • Make sure to UPDATE the fiddle before running it. The closure/window.onload scope disappears – mplungjan Nov 28 '17 at 14:43
  • @JamesThorpe I see now. I thought the question was: why there is a `[[scopes]]` property? – ibrahim mahrir Nov 28 '17 at 14:45
  • Possible duplicate of [Inline event handler not working in JSFiddle](https://stackoverflow.com/questions/5431351/inline-event-handler-not-working-in-jsfiddle) – James Thorpe Nov 28 '17 at 14:47
  • Is console.dir(addTo); really what you want to do? Don't you want console.dir(addTo());? – iquellis Nov 28 '17 at 14:48
  • @JamesThorpe I see that its JSfiddle issue. I am following [this](https://youtu.be/71AtaJpJHw0?t=2m51s) tutorial which says above function forms closure. Can someone give a definitive answer if its true or not. Thanks. – dasfdsa Nov 28 '17 at 14:50

0 Answers0