N
N
neodekvat092015-07-19 06:52:27
JavaScript
neodekvat09, 2015-07-19 06:52:27

Explain why anonymous self-call is needed. functions and closures?

Hello, I recently started learning js (before that, only layout, and php was also fond of). It seems like everything works out, only with understanding and the practice itself is a bit tight.
Please explain why anonymous self-invoking functions are needed (as far as I understand, these are functions that need to be executed once, and so that the local variables of this anon self-invoking func cannot be accessed, but again, I don’t fully understand why, only for security reasons and saving RAM?), and closures, why are they, where can they be applied on a real example?
PS I apologize for such stupid questions, but the truth is, I still can’t find the very understanding, and in all sorts of video tutorials they often just say what she / they are doing, etc.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Solomennikov, 2015-07-19
@neodekvat09

If video lessons are not perceived, then read books, what it is is well described in David Flanagan's book (6th edition), then you may be interested in currying, memoization.
Explained in the book by Marein Haverbeck, casually, but on the fingers.
A lot of interesting things about functions can be found in Stoyan Stefanov's book "Templates" in the Functions section, also in the Object Creation Templates section "Private Properties and Methods".
Functions actually do a lot of interesting things in js, a prototype object, arguments are created, all variables are raised to the beginning of the function, an object is created to store the local variables of the function during its call, and functions also create the so-called "local scope" when called functions use lexical scope, functions are potentially constructors, functions create a namespace (modules) and this is not a complete list, so pay a lot of attention to functions .

_
_ _, 2015-07-19
@AMar4enko

To understand why closures are needed, try writing something more or less serious in JS without them.
IIFEs are used primarily to prevent variables declared in a file from drifting into the global scope.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question