closures

How to access javascript variable stored in function closure from browser console?

落爺英雄遲暮 提交于 2019-12-01 14:20:39
Lets say I have a global function: function Init() { var v = 10; window.GlobalFunction = function() { // Global Function has access to v, because of closure alert(v); } } Init(); How I can get value of v from FireBug console? I can put break point there and see value. However this is not enough for me. I want to build FireFox addon for third party website, so I need access this variable, but can't change source code. If v is a global variable like you say, then you should just be able to type v at the console to get its value. If v is not a global, then you can only access it from within the

Why does resharper suggest “wrap variable in array” for access to modified closure warnings?

余生颓废 提交于 2019-12-01 13:44:36
问题 Given the following (heavily edited, pseudo-)code: int count = 0; thing.Stub(m => m.AddBlah()).WhenCalled(o => count++); thing.Stub(m => m.RemoveBlah()).WhenCalled(o => count--); DoStuff(thing); Assert.AreEqual(1, count); ReSharper provides a warning on count - "Access to modified closure". I understand why I'm getting this warning (the count variable is being modified in two different lambdas, and is likely to have undesirable semantics), but I don't understand ReSharper's advice: "Wrap

Confused about javascript function returning multiple functions with many fat arrow

萝らか妹 提交于 2019-12-01 12:44:30
I have problem with my cs homework. I need to access the x value of the function, but my codes is returning me an empty function instead of with the values I have googled all the currying and closures, but none of them are advanced enough to help me solve my problem const pair = (x, y) => f => f(x, y); // Do not edit this function const head = p => //Answer here console.log(head(pair(1,2))) // Do not edit this my console keeps returning me functions instead when I try all the combinations function(a,b){return a;} You could change head function like this: const pair = (x, y) => f => f(x, y);

need help understanding JS code [closed]

ε祈祈猫儿з 提交于 2019-12-01 12:26:24
Hi I am new to javascript and I cannot understand the following code: var createAdders = function () { var fns = []; for (var i = 1; i < 4; i++) { fns[i] = (function (n) { return i + n; }); } return fns; } var adders = createAdders(); adders[1](7); //11 ?? adders[2](7); //11 ?? adders[3](7); //11 ?? From what I understand 7 is being passed as an argument but createAdders() doesn't assign 7 to any variable so does that mean 7 is being passed to the very next function within createAdders() i.e the anonymous function and assigned to the variable n . Is my logic correct? The code above does seem

Over how much of its enclosing scope does a (javascript) closure close?

随声附和 提交于 2019-12-01 12:26:13
When I have some function that uses variables from its enclosing scope(s) and use that function outside of this scope (these scopes), then this is called a closure. Is there any specification about over "how much" of its enclosing scope(s) a function has to close? (Or, put differently, over "how less" it absolutely needs to close?) Consider: function Outer() { var bigGuy = createSomethingHuge(); var tinyNumber = 42; return (function () { /* CONTENTS */ }); } Or even: function Layer1() { var bigOne = somethingHugePlease(); var Layer2 = function() { var bigToo = morePlease(); var Layer3 =

How to access javascript variable stored in function closure from browser console?

試著忘記壹切 提交于 2019-12-01 12:09:13
问题 Lets say I have a global function: function Init() { var v = 10; window.GlobalFunction = function() { // Global Function has access to v, because of closure alert(v); } } Init(); How I can get value of v from FireBug console? I can put break point there and see value. However this is not enough for me. I want to build FireFox addon for third party website, so I need access this variable, but can't change source code. 回答1: If v is a global variable like you say, then you should just be able to

Confused about javascript function returning multiple functions with many fat arrow

北城余情 提交于 2019-12-01 11:56:54
问题 I have problem with my cs homework. I need to access the x value of the function, but my codes is returning me an empty function instead of with the values I have googled all the currying and closures, but none of them are advanced enough to help me solve my problem const pair = (x, y) => f => f(x, y); // Do not edit this function const head = p => //Answer here console.log(head(pair(1,2))) // Do not edit this my console keeps returning me functions instead when I try all the combinations

Is it possible to get a different scope for a required file?

有些话、适合烂在心里 提交于 2019-12-01 11:17:05
问题 Assume I have this example file called import.js var self; function Test(a,b){ this.a = a; this.b = b; self = this; } Test.prototype.run = function(){ console.log(self.a, self.b) } module.exports = Test When I require the file and create one new 'object' everything works perfectly, but when I create a 2nd object, they both have access to self, only the latter one works. var Test = require('./import.js'); var one = new Test(1,2); one.run() 1 2 var two = new Test(3,4); two.run() 3 4 one.run() 3

Initializing property via closure

萝らか妹 提交于 2019-12-01 11:13:40
I've observed that people sometimes using closures to initialize properties. e.g. instead of lazy var test1: String = String("a string") they use lazy var test2: String = { String("a string") }() What is the benefit/convenience in using closure to initialize property? These two do the same work. Closure initialization comes handy when you need extra code to configure property object. E.g.: lazy var point: CGPoint = { let x = ... let y = ... return CGPoint(x: x, y: y) }() In general, if there is no extra work needed for the lazy variable after the initialization for it, it would be enough do

Calling closure on different object?

馋奶兔 提交于 2019-12-01 11:05:15
Suppose I have this class: class MyClass { int myInt MyClass(myInt) { this.myInt = myInt } def myMethod() { print this.myInt } } And somewhere I have: def myClass1 = new MyClass(1) def myMethodClosure = myClass1.&myMethod def myClass2 = new MyClass(2) Now if I call myMethodClosure() it will call myMethod() on myClass1 instance which will print 1. What I want is to call the same myMethodClosure but on a different instance, in this case on myClass2 so it can print 2. Is this possible? I have tried using setDelegate() , but it does not work. I have also seen that there is field thisObject inside