What underlies this JavaScript idiom: var self = this?

See this article on alistapart.com. (Ed: The article has been updated since originally linked) self is being used to maintain a reference to the original this even as the context is changing. It’s a technique often used in event handlers (especially in closures). Edit: Note that using self is now discouraged as window.self exists and … Read more

What is the purpose of a self executing function in javascript?

It’s all about variable scoping. Variables declared in the self executing function are, by default, only available to code within the self executing function. This allows code to be written without concern of how variables are named in other blocks of JavaScript code. For example, as mentioned in a comment by Alexander: (function() { var … Read more

JavaScript closures vs. anonymous functions

Editor’s Note: All functions in JavaScript are closures as explained in this post. However we are only interested in identifying a subset of these functions which are interesting from a theoretical point of view. Henceforth any reference to the word closure will refer to this subset of functions unless otherwise stated. A simple explanation for … Read more

What is the difference between a ‘closure’ and a ‘lambda’?

A lambda is just an anonymous function – a function defined with no name. In some languages, such as Scheme, they are equivalent to named functions. In fact, the function definition is re-written as binding a lambda to a variable internally. In other languages, like Python, there are some (rather needless) distinctions between them, but … Read more

JavaScript closure inside loops – simple practical example

Well, the problem is that the variable i, within each of your anonymous functions, is bound to the same variable outside of the function. ES6 solution: let ECMAScript 6 (ES6) introduces new let and const keywords that are scoped differently than var-based variables. For example, in a loop with a let-based index, each iteration through … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)