How does a function in a loop (which returns another function) work? [duplicate]

When you assign the function to the click handler, a closure is created. Basically a closure is formed when you nest functions, inner functions can refer to the variables present in their outer enclosing functions even after their parent functions have already executed. At the time that the click event is executed, the handler refers … Read more

How to removeEventListener that is addEventListener with anonymous function?

You can’t. You have to use a named function or store the reference somehow. var handler; function doSomethingWith(param) { handler = function(){ document.write(param); }; document.body.addEventListener(‘scroll’, handler,false); } setTimeout(function() { document.body.removeEventListener(‘scroll’, handler ,false); }, 3000); The best would be to do this in a structured way, so that you can identify different handlers and remove them. … Read more

removeEventListener on anonymous functions in JavaScript

if you are inside the actual function, you can use arguments.callee as a reference to the function. as in: button.addEventListener(‘click’, function() { ///this will execute only once alert(‘only once!’); this.removeEventListener(‘click’, arguments.callee); }); EDIT: This will not work if you are working in strict mode (“use strict”;)

Is it valid to define functions in JSON results?

No. JSON is purely meant to be a data description language. As noted on http://www.json.org, it is a “lightweight data-interchange format.” – not a programming language. Per http://en.wikipedia.org/wiki/JSON, the “basic types” supported are: Number (integer, real, or floating point) String (double-quoted Unicode with backslash escaping) Boolean (true and false) Array (an ordered sequence of values, … Read more

Why do you need to invoke an anonymous function on the same line?

Drop the semicolon after the function definition. (function (msg){alert(msg)}) (‘SO’); Above should work. DEMO Page: https://jsfiddle.net/e7ooeq6m/ I have discussed this kind of pattern in this post: jQuery and $ questions EDIT: If you look at ECMA script specification, there are 3 ways you can define a function. (Page 98, Section 13 Function Definition) 1. Using … Read more

Explain the encapsulated anonymous function syntax

It doesn’t work because it is being parsed as a FunctionDeclaration, and the name identifier of function declarations is mandatory. When you surround it with parentheses it is evaluated as a FunctionExpression, and function expressions can be named or not. The grammar of a FunctionDeclaration looks like this: function Identifier ( FormalParameterListopt ) { FunctionBody … Read more

Location of parenthesis for auto-executing anonymous JavaScript functions?

They’re virtually the same. The first wraps parentheses around a function to make it a valid expression and invokes it. The result of the expression is undefined. The second executes the function and the parentheses around the automatic invocation make it a valid expression. It also evaluates to undefined. I don’t think there’s a “right” … Read more