anonymous-function

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

柔情痞子 提交于 2019-11-26 03:28:17
问题 This question already has an answer here: JavaScript closure inside loops – simple practical example 44 answers I\'ve been trying to assign a function to onclick event of a dynamically created \"a\" tag in JavaScript. All of the tags are created in a loop as follows: for ( var i = 0; i < 4; i++ ) { var a = document.createElement( \"a\" ); a.onclick = function( ) { alert( i ) }; document.getElementById( \"foo\" ).appendChild( a ); } The alerted value for all four links is always \"4\". Pretty

Is it valid to define functions in JSON results?

ぐ巨炮叔叔 提交于 2019-11-26 03:15:45
问题 Part of a website\'s JSON response had this (... added for context): {..., now:function(){return(new Date).getTime()}, ...} Is adding anonymous functions to JSON valid? I would expect each time you access \'time\' to return a different value. 回答1: 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

Anonymous class instance -— is it a bad idea?

泄露秘密 提交于 2019-11-26 02:58:02
问题 In ES6 we can do anonymous class: var entity = class { } But we can also instantiate it: var entity = new class { constructor(name) { this.name = name; } getName() { return this.name; } }(\'Foo\'); console.log(entity.getName()); // Foo What is done behind it, what advantage will it bring and what caveats will it also bring? 回答1: Anonymous class instance — is it a bad idea? Yes, a very bad one. Just as bad as new function() { … } was in ES5. This writing style leads to the creation of a new

How to removeEventListener that is addEventListener with anonymous function?

故事扮演 提交于 2019-11-26 02:19:04
问题 function doSomethingWith(param) { document.body.addEventListener( \'scroll\', function() { document.write(param); }, false ); // An event that I want to remove later } setTimeout( function() { document.body.removeEventListener(\'scroll\', HANDLER ,false); // What HANDLER should I specify to remove the anonymous handler above? }, 3000 ); doSomethingWith(\'Test. \'); 回答1: You can't. You have to use a named function or store the reference somehow. var handler; function doSomethingWith(param) {

Is there a difference between (function() {…}()); and (function() {…})();? [duplicate]

随声附和 提交于 2019-11-26 02:11:24
问题 Possible Duplicate: Location of parenthesis for auto-executing anonymous JavaScript functions? Sometimes I see: (function() { ... }()); and sometimes I see: (function() { ... })(); I see both forms with and without arguments. They both execute the anonymous function. Is there a difference between the two forms? Are there any compelling reasons to use one form over the other? 回答1: There is no practical difference in those two forms, but from a grammatical point of view the difference between

Why is “this” in an anonymous function undefined when using strict?

十年热恋 提交于 2019-11-26 02:07:03
问题 Why is this in an anonymous function undefined when using javascript in strict mode? I understand why this could make sense, but I couldn\'t find any concrete answer. Example: (function () { \"use strict\"; this.foo = \"bar\"; // *this* is undefined, why? }()); Test in a fiddle: http://jsfiddle.net/Pyr5g/1/ Check out the logger (firebug). 回答1: It's because, until ECMAscript 262 edition 5, there was a big confusion if people who where using the constructor pattern , forgot to use the new

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

僤鯓⒐⒋嵵緔 提交于 2019-11-25 22:25:05
问题 I was reading some posts about closures and saw this everywhere, but there is no clear explanation how it works - everytime I was just told to use it...: // Create a new anonymous function, to use as a wrapper (function(){ // The variable that would, normally, be global var msg = \"Thanks for visiting!\"; // Binding a new function to a global object window.onunload = function(){ // Which uses the \'hidden\' variable alert( msg ); }; // Close off the anonymous function and execute it })(); Ok

Python Lambda in a loop

梦想与她 提交于 2019-11-25 21:46:37
问题 Considering the following code snippet : # directorys == {\'login\': <object at ...>, \'home\': <object at ...>} for d in directorys: self.command[\"cd \" + d] = (lambda : self.root.change_directory(d)) I expect to create a dictionary of two function as following : # Expected : self.command == { \"cd login\": lambda: self.root.change_directory(\"login\"), \"cd home\": lambda: self.root.change_directory(\"home\") } but it looks like the two lambda function generated are exactly the same : #

Location of parenthesis for auto-executing anonymous JavaScript functions?

妖精的绣舞 提交于 2019-11-25 21:44:29
问题 I was recently comparing the current version of json2.js with the version I had in my project and noticed a difference in how the function expression was created and self executed. The code used to wrap an anonymous function in parenthesis and then execute it, (function () { // code here })(); but now it wraps the auto-executed function in parenthesis. (function () { // code here }()); There is a comment by CMS in the accepted answer of Explain JavaScript’s encapsulated anonymous function

Explain the encapsulated anonymous function syntax

故事扮演 提交于 2019-11-25 21:44:14
问题 Summary Can you explain the reasoning behind the syntax for encapsulated anonymous functions in JavaScript? Why does this work: (function(){})(); but this doesn\'t: function(){}(); ? What I know In JavaScript, one creates a named function like this: function twoPlusTwo(){ alert(2 + 2); } twoPlusTwo(); You can also create an anonymous function and assign it to a variable: var twoPlusTwo = function(){ alert(2 + 2); }; twoPlusTwo(); You can encapsulate a block of code by creating an anonymous