closures

why is IIFE needed to create a new scope?

谁都会走 提交于 2019-12-06 04:05:40
From You Don't Know JS : for (var i=1; i<=5; i++) { setTimeout( function timer(){ console.log( i ); }, i*1000 ); } gives 6 6 6 6 6 but using an IIFE like so for (var i=1; i<=5; i++) { (function(){ var j = i; setTimeout( function timer(){ console.log( j ); }, j*1000 ); })(); } gives 1 2 3 4 5 My question: why doesn't for (var i=1; i<=5; i++) { setTimeout( function timer(){ var j = i; console.log( j ); }, i*1000 ); } or for (var i=1; i<=5; i++) { function timer() { var j = i; console.log(j); } setTimeout(timer, i*1000 ); } work like the IIFE example? It seems to me they both have a function

JavaScript YUI3 using global variables?

天大地大妈咪最大 提交于 2019-12-06 03:40:24
I can't work out how to update a global variable from within YUI3. Consider the following code: window.myVariable = 'data-one'; var yuiWrap = YUI().use('node',function(Y) { console.log(window.myVariable); // 'data-one' window.myVariable = 'data-two'; console.log(window.myVariable); // 'data-two' }); console.log(window.myVariable); // 'data-one' Can anyone explain this to me? It's causing me a lot of trouble. Why can window.myVariable be accessed but not properly updated from within a YUI3 block? I think it might have something to do with Closures but I don't understand why Closures should

Swagger generating javascript-closure-angular-client

三世轮回 提交于 2019-12-06 03:38:14
I am new to swagger, I have generated a javascript closure angular client from the swagger's online editor, it gave me a DefaultAPI.js and other JS files that match my objects definitions. I've searched in swagger docs and on the net for a way to use the generated files in an angular application but didn't find any explanation, any clue ? 来源: https://stackoverflow.com/questions/40936437/swagger-generating-javascript-closure-angular-client

Closure Definition and Example

北城余情 提交于 2019-12-06 03:29:36
问题 what is closure and its correspondence example ? I have research a lot and could not understand. Please explain in generic programming language concept aspect and specific programming language aspect. Please help. Thanks. 回答1: Here is how I think of a closure.... A function object consists of two things. The first thing is the code for the function, the second is the scope in which it executes. In a closure, the scope in which the function executes and the code are detached from each other.

Store state of a JavaScript Object

拜拜、爱过 提交于 2019-12-06 03:17:54
问题 Im trying to store the stats of 'this' in my javscript object so that later on in my application I can return 'this' to a previous state. I thought I could accomplish using a closure but so far I haven't successful. My idea was to do something like this function SavedFeature() { var self = this; this.savedItem; this.storeState = function() { this.savedItem = storeClosure(); } function storeClosure() { var closure = self; return function() { return closure; }; }; //other things the user can

JavaScript Closure - Eval() and capturing variables in Eval()'s scope

ぃ、小莉子 提交于 2019-12-06 03:10:00
My question is regarding JavaScript Closures and the Eval() function. I have some code that looks like this, there is also some other jQuery plugin related code taht is not shown. I can update the question with more code if needed. var _CurrentDataRowIndex = 1; function LoadParsedRowTemplate(rowData, type) { var result; var asyncbinder = /&\{[\S\s]*?}&/g; while ((result = asyncbinder.exec(template)) != null) { replacement = eval("$.fn.ScreenSetup." + result[0].substring(2, result[0].length - 3) + ", rowData, " + _CurrentDataRowIndex + ")"); template = template.replace(result[0], "AsyncBind!!")

When is this scope/closure being garbage collected in javaScript?

◇◆丶佛笑我妖孽 提交于 2019-12-06 02:35:50
I am doing a course which is going through scope/closures and briefly mentions garbage collection. During the course a question is posed: How long does the scope stay around? And the answer was—until there's no longer any references to it. Yep, so what we basically said was, a closure is kind of like a reference to a hidden scope object. So as long as there's some function that still has a closure over the scope, that scope's going to stay around. But as soon as that closure goes away, scope can get garbage collected. " var sum = function sumHndlr(x, y) { if (y !== undefined) { return x + y; }

Why are closures better than global variables for preserving variables?

不羁的心 提交于 2019-12-06 01:57:58
问题 I understand how closures work within JavaScript, but my question is why would you go through all the trouble of making a closure to preserve a variable? Couldn't you just make the variable global? Or would that clutter up the global scope and make your code be prone to errors. 回答1: It's a scoping issue. Global variables are just that: Global, to everyone . With closures, the scope (visibility) of the variables can be better controlled, which means the possible unintended side effects can be

Javascript scope referencing outer object

不打扰是莪最后的温柔 提交于 2019-12-06 01:50:40
Basically, I use a meta-class framework called Joose for Javascript that allows me to make use of a more elegant class syntax - but I don't know how I might go about referencing the scope of the object from within deeper methods of the class declaration. I also use require.js for dependemcy management... Here's an example class definition: define([ 'jquery', 'handlebars', ], function($, Handlebars){ var MyClass = Class("MyClass", { //inheritance isa: SuperClass, //instance vars has: { hello:{ is: 'r', init: 'Hi There!', }, someVarToBeSetUsingAjax:{ is: 'rw', init: false, }, }, //methods

Reference Instance Variables in Javascript Constructor

我的未来我决定 提交于 2019-12-06 00:50:37
I'm trying to maintain state on an object by doing something like this: obj = function() { this.foo = undefined; this.changeState = function () { (function () { this.foo = "bar" })(); // This is contrived, but same idea. }; }; I want to set the instance variable foo to "bar" when I call the changeState method. For instance: o = new obj(); o.changeState(); alert(o.foo); // This should say "bar" As far as I can tell, what is happening is that "this" in the inner anonymous function is pointing to window. I'm not sure what's going on. Am I on the right track? Is there a better approach? Unless you