-8

enter image description here

I am a javascript beginner, and I do not quite understand this code in the picture, can somebody explain a little ? Thanks!!

Rao
  • 20,781
  • 11
  • 57
  • 77
htjoseph
  • 9
  • 3
  • 2
    Hello, and welcome to Stack Overflow. Please avoid posting text (including code) as an image in the future. Also, to understand exactly what is going on in the example, read up on [closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures), as I assume that's the part you have problems with. – Amadan Apr 06 '16 at 01:18
  • 2
    That piece of code looks like it comes from some kind of tutorial about functional programming (or higher-order functions, or closures, or something like that). Isn't there any explanation about what it does? Where did you find it? – Thilo Apr 06 '16 at 01:19
  • Related: http://stackoverflow.com/a/18234552/772035 – Paul Apr 06 '16 at 01:47

1 Answers1

0

The easiest way to explain the code is to fill in the values during each call.

The function plusGenerator is a function that takes an offset and then returns another function that returns the result of adding offset to another number. When you call plusGenerator(2), the function that is returned looks like:

var addTwo = function(x) { return x + 2; };

You then call addTwo(15) which returns 17.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536