-1

When I have a setInterval set up like this:

var intervalTime = 200;
var i = 0;
var elements = document.getElementsByTagName('elements');
var elementsLength = elements.length;

var enterElements = setInterval(function(){

    elements[i].style.transform = 'translate(0,0)';
    i++;

    if (i > (elementsLength - 1)) {
        clearInterval(enterElements);
    }

    },intervalTime);

It works!

But I thought that functions have scope?

I understand that intervalTime should be parsed correctly, but how can the anonymous function inside setInterval() access

  • elements
  • i
  • elementsLength

I know that when I write a named function, that function cannot then access variables declared outside of itself.

Are anonymous functions that different from named functions?

Rounin
  • 27,134
  • 9
  • 83
  • 108
  • 3
    Named functions can indeed reference variables declared outside the function. They're not much different from anonymous functions; barely different, in fact. – Pointy Feb 14 '16 at 23:31
  • 2
    "*I know that when I write a named function, that function cannot then access variables declared outside of itself*" - that is not true (not even a little bit) – Amit Feb 14 '16 at 23:34
  • Aha. So this is a fundamental difference between function scope in PHP and javascript then, is it? Because I'm nearly 100% certain that a named function in PHP cannot access variables declared outside of itself (unless they are passed as arguments...) – Rounin Feb 14 '16 at 23:35
  • 3
    You can kind of think of scope as between curly braces `{...}`. Essentially, you can go *in*, but not out. So `foo` in `function() { var foo = 'bar'; }` cannot be accessed outside of those braces in the basic sense. – Tomanow Feb 14 '16 at 23:37
  • Thanks @Tomanow (and everyone else!) - that's helpful. So... function scope works differently in `javascript` (deeper scope environments can access variables from higher scope environments) than in `PHP` (no access from within any scoped environment to variables residing in any other scoped environment). Is that right? If someone familiar with both `PHP` and `javascript` can confirm that's correct, I'll be most grateful. – Rounin Feb 14 '16 at 23:44
  • the gotcha of `setInterval` and `setTimeout` is that the context of `this` will be `window` unless you `bind` or create a closure on the anaonymous or named function scope. – jmunsch Feb 14 '16 at 23:47
  • @jm_____ that's true, but the context doesn't matter for code like that in the OP, because it doesn't refer to `this` at all. – Pointy Feb 14 '16 at 23:57
  • This has to do with scope, have a look at this [question](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript). – Pier-Luc Gendreau Feb 15 '16 at 00:03
  • 1
    @Pointy ah, you are right. edit: "As a sidenote: another gotcha ..." – jmunsch Feb 15 '16 at 00:08
  • 2
    Yes, that is correct. In PHP the function scope does not inherit from the global scope. You can however use the `global` keyword to "import" a global variable in a function. In JavaScript the function scope inherits the scope where the function is created. That is usually the global scope, but it can be another function scope, in which case that has inherited the global scope. – Guffa Feb 15 '16 at 00:24
  • 1
    In Javascript every single function that exists (anonymous or not) can see the global scope. All functions are defined somewhere in the code, and that location determines their scope. – user2867288 Feb 15 '16 at 01:06
  • Thanks @Guffa - that was exactly the clarification I was looking for! – Rounin Feb 15 '16 at 14:41
  • Thanks for downvoting the question, whoever that was. I still think it's a pertinent question for anyone (not just me) getting to grips with `javascript` from a background in intermediate `PHP`, so I'm going to leave the question up for now, rather than delete it. – Rounin Feb 16 '16 at 13:27

1 Answers1

1

I know that when I write a named function, that function cannot then access variables declared outside of itself.

How do you know that? See MDN:

However, a function can access all variables and functions defined inside the scope in which it is defined. In other words, a function defined in the global scope can access all variables defined in the global scope. A function defined inside another function can also access all variables defined in its parent function and any other variable to which the parent function has access.

  • _How do you know that?_ The answer to your question is _Because... PHP_. Now I'm aware that's a stupid answer (at least I am now, thanks to @Guffa's comment above) but that is the reason why I wrote that erroneous assertion in my question. – Rounin Feb 15 '16 at 14:43