anonymous-function

JavaScript: access variables inside anonymous function from the outside

。_饼干妹妹 提交于 2019-11-29 09:14:24
Say I have this anonymous function: (function(window){ var private = 'private msg'; function sayit() { alert(private) // works } document.body.onclick = sayit; // works })(window); // private shouldn't be accessible here Is this how JavaScript should behave? That is, there is no way to access private from anywhere outside of that anonymous function? If so, is it possible to find some kind of hack to access private from the outside, leaving the code the way it is? Yes, this is how Javascript lets you have 'private' variables (hidden in a function scope). No, there's no hack available to access

detachEvent not working with named inline functions

情到浓时终转凉″ 提交于 2019-11-29 08:32:08
I ran into a problem in IE8 today (Note that I only need to support IE) that I can't seem to explain: detachEvent wouldn't work when using a named anonymous function handler. document.getElementById('iframeid').attachEvent("onreadystatechange", function onIframeReadyStateChange() { if (event.srcElement.readyState != "complete") { return; } event.srcElement.detachEvent("onreadystatechange", onIframeReadyStateChange); // code here was running every time my iframe's readyState // changed to "complete" instead of only the first time }); I eventually figured out that changing

Sending anonymous functions through socket.io?

£可爱£侵袭症+ 提交于 2019-11-29 07:46:07
I want to create a client-side function that can receive and execute arbitrary commands using client-side variables. I will be sending these functions from my server by using socket.io to send a JSON object containing an anonymous function which will be my command. It looks something like the following: //client side socket.on('executecommand', function(data){ var a = "foo"; data.execute(a); //should produce "foo" }); //server side socket.emit('executecommand', {'execute': function(param){ console.log(param); }}); Yet, when I tried it out, the client side received an empty json object ( data =

PHP use() function for scope?

白昼怎懂夜的黑 提交于 2019-11-29 06:46:37
I have seen code like this: function($cfg) use ($connections) {} but php.net doesn't seem to mention that function. I'm guessing it's related to scope, but how? use is not a function, it's part of the Closure syntax . It simply makes the specified variables of the outer scope available inside the closure. $foo = 42; $bar = function () { // can't access $foo in here echo $foo; // undefined variable }; $baz = function () use ($foo) { // $foo is made available in here by use() echo $foo; // 42 } For example: $array = array('foo', 'bar', 'baz'); $prefix = uniqid(); $array = array_map(function (

Use variables inside an anonymous function, which is defined somewhere else

。_饼干妹妹 提交于 2019-11-29 06:18:48
When using anonymous functions in PHP, you can easily use variables from right outside of its scope by using the use() keyword. In my case the anonymous functions are already defined somewhere, but called later on (somewhere else) in a class. The following piece of code is to illustrate the idea: <?php $bla = function ( $var1 ) use ($arg) { echo $var1; }; class MyClass { private $func; public function __construct ( $func ) { $this->func = $func; } public function test ( $arg ) { $closure = $this->func; $closure ( 'anon func' ); } } $c = new MyClass($bla); $c->test ( 'anon func' ); What i'm

Anonymous Scala function syntax

穿精又带淫゛_ 提交于 2019-11-29 00:40:02
问题 I'm learning more about Scala, and I'm having a little trouble understanding the example of anonymous functions in http://www.scala-lang.org/node/135. I've copied the entire code block below: object CurryTest extends Application { def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)) xs.head :: filter(xs.tail, p) else filter(xs.tail, p) def modN(n: Int)(x: Int) = ((x % n) == 0) val nums = List(1, 2, 3, 4, 5, 6, 7, 8) println(filter(nums, modN(2)))

Slow performance using anonymous functions in MATLAB… have others noticed this?

不想你离开。 提交于 2019-11-29 00:14:23
In order to refactor my MATLAB code, I thought I'd pass around functions as arguments (what MATLAB calls anonymous functions), inspired by functional programming. However, it seems performance is hit quite severely. In the examples below, I compare different approaches. (The code snippet is wrapped in a function in order to be able to use subfunctions) The result I get is 0 seconds for direct, almost 0 seconds using a subfunction, and 5 seconds using anonymous functions. I'm running MATLAB 7.7 (R2007b) on OS X 10.6, on a C2D 1.8 GHz. Can anyone run the code and see what they get? I'm

Clojure: returning a vector from an anonymous function

微笑、不失礼 提交于 2019-11-28 21:16:56
I wrote a small anonymous function to be used with a map call. The function returns a vector containing a column name and column value from a SQL result set query. Here is the function (input is the column name): (fn [name] [(keyword name) (.getObject resultset name)]) This works fine, however when I tried to use a "simplified" version of the anonymous function, I got an error: #([(keyword %) (.getObject resultset %)]) java.lang.IllegalArgumentException: Wrong number of args (0) passed to: PersistentVector Here is the map call: (into {} (map (fn [name] [(keyword name) (.getObject resultset

Anonymous function vs normal function

那年仲夏 提交于 2019-11-28 21:16:42
Just out of interest, are there any speed/functionality differences between function foo(bar) { alert("foo" + bar); } and var foo = function(bar) { alert("foo" + bar); }; There are no significant speed differences. ( Test ) There are functionality differences. Function declarations (like your first) and function expressions (like your second) are processed at different times. They have different effects on the scope in which they occur. Your first function has a true name , your second does not in ES5 and earlier, your second does not; in ES6/ES2015, it does, because the specification says

Why use anonymous function? [duplicate]

99封情书 提交于 2019-11-28 20:49:49
问题 Possible Duplicate: How do you use anonymous functions in PHP? Why should i use an anonymous function? I mean, what's the real deal using it? I just don't really get this. I mean, you use function to make the code more clean or to use it more than once. But Anonymous functions just don't do neither the first nor the second. I googled them and i couldn't find anyone asking the same problem. 回答1: I would say that anonymous functions show their beauty when there is good library classes/functions