closures

Exposing a method which is inside a closure

好久不见. 提交于 2019-12-03 06:19:36
When we are creating a method inside a closure it becomes private to that closure and can't be accessed until we expose it in some way. How can it be exposed? You can return a reference to it... var a = function() { var b = function() { // I'm private! alert('go away!'); }; return { b: b // Not anymore! }; }; See it on jsFiddle . You could also bind it to the window object . But I prefer the method above, otherwise you are exposing it via a global variable (being a property of the window object). You need to pass it to the outside in some manner. Example: http://jsfiddle.net/patrick_dw/T9vnn/1

Javascript - difference between namespace vs. closure?

痴心易碎 提交于 2019-12-03 06:19:15
问题 In Javascript, what's the difference between a namespace and a closure? They seem very similar to me. EDIT Specifically, this article discusses namespaces and closures, and has sentences like Now, we’re still going to have situations where we’ll want to declare variables that don’t naturally fit into a namespaced object structure. But we don’t want those variables to have a global scope. This is where self-invoking functions come in. It goes on to give what looks a lot like a closure, as an

Arguments to JavaScript Anonymous Function

青春壹個敷衍的年華 提交于 2019-12-03 06:18:31
for (var i = 0; i < somearray.length; i++) { myclass.foo({'arg1':somearray[i][0]}, function() { console.log(somearray[i][0]); }); } How do I pass somearray or one of its indexes into the anonymous function ? somearray is already in the global scope, but I still get somearray[i] is undefined The i in the anonymous function captures the variable i , not its value . By the end of the loop, i is equal to somearray.length , so when you invoke the function it tries to access an non-existing element array. You can fix this by making a function-constructing function that captures the variable's value:

Scope chain look-up vs prototype look-up - Which is when

旧城冷巷雨未停 提交于 2019-12-03 06:10:19
问题 If a variable is not available in a function when it's needed, then it's being looked for in the scope chain (which is a closure), but other times it's being searched for in the prototype chain. I am trying to wrap my head around which is happening when. I was wondering if someone could kindly clear the mist for me, or refer me to some literature discussing this topic specifically. For example, would I be right saying that: - Objects and therefore public variables tied to the context (this

What exactly is a PowerShell ScriptBlock

时光毁灭记忆、已成空白 提交于 2019-12-03 05:58:34
A PowerShell ScriptBlock is not a lexical closure as it does not close over the variables referenced in its declaring environment. Instead it seems to leverage dynamic scope and free variables which are bound at run time in a lambda expression. function Get-Block { $b = "PowerShell" $value = {"Hello $b"} return $value } $block = Get-Block & $block # Hello # PowerShell is not written as it is not defined in the scope # in which the block was executed. function foo { $value = 5 function bar { return $value } return bar } foo # 5 # 5 is written $value existed during the evaluation of the bar

Understanding javascript closures and memory usage

隐身守侯 提交于 2019-12-03 05:57:20
问题 EDIT : This is just a simple example to demontrate the concern I have with a much larger program. I wouldn't use this actual code for anything :) If I run this - <!DOCTYPE html> <html> <head> <script> function update(amount, win, data) { win.innerText = 'Count is ' + amount; setTimeout(function() { update(amount + 1, win, {data: 'something'})}, 1000); } window.onload = function() { var win = document.getElementById('item'); update(0, win, 0); } </script> </head> <body> <div id="item"></div> <

Javascript performance with closure

一世执手 提交于 2019-12-03 05:55:27
问题 var name = function(n) { var digits = ['one','two','three','four']; return digits[n]; } var namenew = (function() { digits = ['one','two','three','four']; return function(n) { return digits[n]; } }()); Both the versions result in the same output, however it is said that the second version is much faster than the first version. As I understand, first version executes the function everytime where as the second version stores the result of execution. That is what confuses me as a functional

Private field captured in anonymous delegate

半世苍凉 提交于 2019-12-03 05:54:56
class A { public event EventHandler AEvent; } class B { private A _foo; private int _bar; public void AttachToAEvent() { _foo.AEvent += delegate() { ... UseBar(_bar); ... } } } Since delegate captures variable this._bar , does it implicitly hold to the instance of B ? Will instance of B be referenced through the event handler and captured variable by an instance of A ? Would it be different if _bar was a local variable of the AttachToAEvent method? Since in my case an instance of A lives far longer and is far smaller than an instance of B , I'm worried to cause a "memory leak" by doing this.

Closure in Javascript with multiple brackets

独自空忆成欢 提交于 2019-12-03 05:53:27
问题 Could any one explain how this function alert, when more no of brackets of parameters are passed. I could not able to understand it clearly. function sum(a) { var sum = a function f(b) { sum += b return f } f.toString = function() { return sum } return f } alert( sum(1)(2) ) // 3 alert( sum(5)(-1)(2) ) // 6 alert( sum(6)(-1)(-2)(-3) ) // 0 alert( sum(0)(1)(2)(3)(4)(5) ) // 15 回答1: The first time your function is called, the first value is stored in sum . After that function f(b) will be

Can't make weak reference to closure in Swift

会有一股神秘感。 提交于 2019-12-03 05:44:32
Update: I tried writing it without making it weak, and there doesn't seem to be a leak. So maybe the question is no longer necessary. In Objective-C ARC, when you want to have a closure be able to use itself inside of the closure, the block cannot capture a strong reference to itself, or it will be a retain cycle, so instead you can make the closure capture a weak reference to itself, like so: // This is a simplified example, but there are real uses of recursive closures int (^fib)(int); __block __weak int (^weak_fib)(int); weak_fib = fib = ^(int n) { if (n < 2) return n; else return weak_fib