0

I read in Java also that anonymous functions introduces a new scope within the enclosing function whereas lambda expressions are block-local.Does it mean the above.I am confused with the following example:

var timer = {
    seconds: 0,
    start() {
        setInterval(() => {
            this.seconds++
        }, 1000)
    }
}
// When an item is searched, it starts from the anonymous function
// and this doesn't mean the same as parent this.
// This means that we can access this .seconds by first going to
// its enclosing function in the arrow expression.
timer.start()

setTimeout(function () {
    console.log(timer.seconds)
}, 3500)

Here this.seconds will introduce a new scope of this in the enclosing function if it is an anonymous function.Am I right? Static (Lexical) Scoping vs Dynamic Scoping (Pseudocode)

craftsman
  • 15,133
  • 17
  • 70
  • 86
  • The `this` keyword is not called "scope" if that is what you are looking for. – Bergi Aug 18 '17 at 20:47
  • What do you mean by "anonymous function" and "lamda expression"? Function expressions and arrow functions can both be anonymous. – Bergi Aug 18 '17 at 20:49
  • Kindly explain me something – Deepak Gupta Aug 18 '17 at 20:50
  • Yes,can you explain me scoping in classes – Deepak Gupta Aug 18 '17 at 20:52
  • What does this have to do classes? Of course I could explain you anything, but I won't if you don't tell what you don't understand. Put some comments in your code. – Bergi Aug 18 '17 at 20:56
  • What do you mean by "*When an item is searched*"? And by "anonymous function", you refer to the `() => { … }` one? – Bergi Aug 18 '17 at 21:05
  • an anonymous function cannot access its outer elements but arrow expression can,But I read in some book about lexical scoping of arrow expressions – Deepak Gupta Aug 18 '17 at 21:11
  • 1
    Every function creates its own scope, no matter if it is anonymous or an arrow function, or not, and they always can access the outer scope. Scope is something you literally write to the script file, and it can't be programmatically changed. – Teemu Aug 18 '17 at 21:16
  • Thank you very much ,but kindly explain about the first comment – Deepak Gupta Aug 18 '17 at 21:17
  • @DeepakGupta I still don't get what you mean by "outer elements". Variables? – Bergi Aug 18 '17 at 21:25
  • Probably you've a confusion between the scope and the context. Maybe [the docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) helps? – Teemu Aug 18 '17 at 21:35

1 Answers1

0

The difference here is this. Arrow functions do not change the value of this, while functions created with the function keyword do.

That is why there is a difference between the code you have above and the following code:

var timer = {
    seconds: 0,
    start() {
        setInterval(function(){
            this.seconds++
        }, 1000)
    }
}
// When an item is searched, it starts from the anonymous function
// and this doesn't mean the same as parent this.
// This means that we can access this .seconds by first going to
// its enclosing function in the arrow expression.
timer.start()

setTimeout(function () {
    console.log(timer.seconds)
}, 3500)

In this code, function(){ overwrites the value of this to itself, so there is no seconds variable anymore.

There is no difference in scope (which variables the functions have access to) between traditional anonymous functions and arrow functions, only that traditional anonymous functions create a new this and arrow functions do not.

Stephen S
  • 354
  • 1
  • 4
  • 21