closures

Using Object.DefineProperty and accessing a variable in private scope

和自甴很熟 提交于 2020-01-24 06:23:05
问题 The following doesn't work, from my getter, I can't see _nickname defined in the 'class' Person. var Person = function (args) { var _nickname = ''; if (args === undefined || args === null) { return; } if (args.nickname !== undefined && args.nickname !== null) { _nickname = args.nickname; } } Object.defineProperty(Person.prototype, "nickname", { get : function () { return _nickname; } }); var x = new Person({ nickname : 'bob' }); console.log(x.nickname); How should one go about accomplishing

Haskell - is this a closure?

我是研究僧i 提交于 2020-01-24 03:10:33
问题 There is a piece of source-code that originated in an answer to another one of my questions, infFromPrefix :: Eq a => ([a] -> [a]) -> [a] -> [a] infFromPrefix rules prefix = inf where inf = prefix ++ case stripPrefix prefix (rules inf) of Just suffix -> suffix Nothing -> error "Substitution does not preserve prefix" where I am pretty sure that inf must be a closure as it has access to variables from its enclosing scope in the sense that it uses the parameters passed to infFromPrefix , but am

Accessing variable in another function, returns undefined - JavaScript

 ̄綄美尐妖づ 提交于 2020-01-24 01:02:48
问题 I am trying to access a variable which exists in another function, but I am not able to, it gives me undefined for the function through which (getMess() as below) I am doing that. As per the code below, I want the "value1" accessed through myfunction1, as shown below. Code: var namespace ={ myfunction1: function(){ namespace.myfunction2.getMess(); // I need to access value1 here in this function }, myfunction2: function(message1,message2){ var value1 = message1; var value2 = message2; return{

Closures in Java

元气小坏坏 提交于 2020-01-23 13:12:42
问题 This example is a simulation of closures in JavaScript (I don't know JS): public class Lambda { public static void main(String[] args) { Supplier generator = Lambda.generator(); System.out.println(generator.get()); System.out.println(generator.get()); System.out.println(generator.get()); } static Supplier<Integer> generator() { Integer arr[] = {0}; return () -> ++arr[0]; } } The output is 1 2 3. Usually the lifespan of local method variables is limited by the method execution time. But in

Closures in Java

*爱你&永不变心* 提交于 2020-01-23 13:12:30
问题 This example is a simulation of closures in JavaScript (I don't know JS): public class Lambda { public static void main(String[] args) { Supplier generator = Lambda.generator(); System.out.println(generator.get()); System.out.println(generator.get()); System.out.println(generator.get()); } static Supplier<Integer> generator() { Integer arr[] = {0}; return () -> ++arr[0]; } } The output is 1 2 3. Usually the lifespan of local method variables is limited by the method execution time. But in

closure in for-loop > different tries failed

烈酒焚心 提交于 2020-01-22 03:42:09
问题 I want to create a 2-dimensional array with an index-number in each first element. EDIT: thx a lot so far.. @carl: I did so much function creation just to show the kind of tries I did.. jonhopkins idea gave rise to this: this works: $('#create_indexed_array').click(function() { var new_array = [[9,9],[9,9],[9,9],[9,9],[9,9]]; for (var i = 0; i < 5; i++) { new_array[i][0] = i; } alert(JSON.stringify(new_array)); }); BUT this works not: $('#create_indexed_array').click(function() { var new

Setting event handlers using a for loop [duplicate]

陌路散爱 提交于 2020-01-22 02:51:08
问题 This question already has answers here : How to generate event handlers with loop in Javascript? [duplicate] (3 answers) Closed 6 years ago . I was messing around with some javascript, on jsfiddle, and ran into a strange issue. I can't seem to figure out why I am unable to set onclick event handlers via a for loop: html: <table border="1" cellspacing="1" width="500"> <tr id="headerRow"> <td>Header1</td> <td>Header2</td> <td>Header3</td> <td>Header4</td> </tr> <tr> <td>books</td> <td>red</td>

How to use a reference to a FnOnce closure?

你说的曾经没有我的故事 提交于 2020-01-21 20:40:10
问题 I have a function which needs to pass a closure argument recursively use std::cell::RefCell; use std::rc::Rc; pub struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } pub fn pre_order<F>(root: Option<Rc<RefCell<TreeNode>>>, f: F) where F: FnOnce(i32) -> (), { helper(&root, f); fn helper<F>(root: &Option<Rc<RefCell<TreeNode>>>, f: F) where F: FnOnce(i32), { match root { Some(node) => { f(node.borrow().val); helper(&node.borrow().left, f);

How to use a reference to a FnOnce closure?

纵饮孤独 提交于 2020-01-21 20:35:06
问题 I have a function which needs to pass a closure argument recursively use std::cell::RefCell; use std::rc::Rc; pub struct TreeNode { val: i32, left: Option<Rc<RefCell<TreeNode>>>, right: Option<Rc<RefCell<TreeNode>>>, } pub fn pre_order<F>(root: Option<Rc<RefCell<TreeNode>>>, f: F) where F: FnOnce(i32) -> (), { helper(&root, f); fn helper<F>(root: &Option<Rc<RefCell<TreeNode>>>, f: F) where F: FnOnce(i32), { match root { Some(node) => { f(node.borrow().val); helper(&node.borrow().left, f);

Printing out the fibonacci series

杀马特。学长 韩版系。学妹 提交于 2020-01-21 08:56:50
问题 I am trying to write a simple Python program. It's supposed to return a a closure that returns successive fibonacci numbers: def fibGen(): n_1 = 0 n_2 = 0 n = 1 def fib(): if n_1 ==0 and n_2 ==0: n_1 = 1 return n else: n = n_1 + n_2 n_2 = n_1 n_1 = n return n return fib f = fibGen() for i in range(0,10): print(f()) I get this error at run time: UnboundLocalError: local variable 'n_1' referenced before assignment EDIT: In my original post, I had not included n = 1 in the definition of fibGen