anonymous-function

Modify arguments in self executing function

吃可爱长大的小学妹 提交于 2019-12-06 05:41:15
I want to be able to modify the arguments passed to a self executing function. Here is some sample code: var test = 'start'; (function (t) {t = 'end'} )(test); alert(test) //alerts 'test' And here is a fiddle . The variable test has not changed. How can I alter it, as in pass-by-reference? Pass in an object , it is pass-by-reference : var test = { message: 'start' }; (function (t) {t.message = 'end'} )(test); alert(test.message) FYI, Array is also pass-by-reference . You cannot do that (well, precisely that) in JavaScript. You can do something like this, however: var testBox = { test: "hello"

Calling anonymous functions defined as object variables in php [duplicate]

允我心安 提交于 2019-12-06 04:47:00
问题 This question already has answers here : Calling closure assigned to object property directly (12 answers) Closed 6 years ago . I have php code like: class Foo { public $anonFunction; public function __construct() { $this->anonFunction = function() { echo "called"; } } } $foo = new Foo(); //First method $bar = $foo->anonFunction(); $bar(); //Second method call_user_func($foo->anonFunction); //Third method that doesn't work $foo->anonFunction(); Is there a way in php that I can use the third

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

What is the type of a lambda function?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 23:03:36
问题 In C++0x, I'm wondering what the type is of a lambda function. Specifically: #include<iostream> type1 foo(int x){ return [x](int y)->int{return x * y;}; } int main(){ std::cout<<foo(3)(4);//would output 12 type2 bar = foo(5); std::cout<<bar(6);//would output 30 return 0; } What do I need to replace type1/type2 with to get the above to work? Hopefully you can see what I'm trying to accomplish, so even if this isn't possible by a direct replacement of type1 and type2, perhaps you can guide me

jQuery toggle class with delay works only once

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 22:05:32
I am clearly missing something fundamental when it comes to jQuery, anonymous functions, and delays. The following code works only ONCE per page load (it will add the class, then remove it after 1 second, and if i click again, it will add the class, but will NEVER remove the class for the duration of the page, unless I reload the page): var jElement = $(currElem); jElement.addClass("highlight") .delay(1000) .queue(function(){ $(this).removeClass("highlight"); }); HOWEVER, if I add the (non-existant) function call as a parameter, AND I call it in my anonymous function, then the add/remove class

Scala Generic Function Values (Anonymous Function) - Missing Parameter Type (Error)

﹥>﹥吖頭↗ 提交于 2019-12-05 20:09:15
问题 I'm new to Scala ( Scala code runner version 2.7.7.final ), and I really don't understand why it requires the caller to provide the parameter type when we are using high order functions. In the sample below, I have one stand alone object ( Util ) that has one function. But in the Main block, the caller must pass the parameter type to the anonymous function. Why does Scala not infer the type of the function from the Array type (i.e. String )? Is there any way to do that ? object Util { // Just

Closure objects within arrays before PHP 5.3

冷暖自知 提交于 2019-12-05 15:16:57
I know it's possible to do the following with PHP 5.3 (anonymous functions), but is there a similar alternative in older PHP version (pre-5.3)? $exampleArray = array( 'func' => function() { echo 'this is an example'; } Is it possible to do this with __call or typecasting the function as an (object) first? Also, I tried making the function un-anonymous by giving it a name, but this didn't seem to work. If you want to create anonymous in PHP < 5.3, you can use create_function function. Also Here is interesting information about callbacks (may be usefull). Example using create_function # This

PHP: Pass anonymous function as argument

我怕爱的太早我们不能终老 提交于 2019-12-05 15:02:21
问题 Is it possible to pass an anonymous function as an argument, and have it execute immediately, thus passing the function's return value? function myFunction(Array $data){ print_r($data); } myFunction(function(){ $data = array( 'fruit' => 'apple', 'vegetable' => 'broccoli', 'other' => 'canned soup'); return $data; }); This throws an error due to the Array type-hint, complaining of an object being passed. Alright, if I remove the type-hint, it of course spits out Closure Object , rather than the

Matlab - for loop in anonymus function

北慕城南 提交于 2019-12-05 13:51:15
问题 I'm quite new to matlab, but I know how to do both for loops and anonymous functions. Now I would like to combine these. I want to write: sa = @(c) for i = 1:numel(biscs{c}), figure(i), imshow(biscs{c}{i}.Image), end; But that isn't valid, since matlab seem to want newlines as only command-seperator. My code written in a clear way would be (without function header): for i = 1:numel(biscs{c}) figure(i) imshow(biscs{c}{i}.Image) end I look for a solution where either I can write it with an

Multiline anonymous function in Matlab? [duplicate]

百般思念 提交于 2019-12-05 13:45:47
This question already has an answer here: How to execute multiple statements in a MATLAB anonymous function? 6 answers Is it possible to create multiline anonymous function in Matlab? There are no appropriate examples in documentation , but also no direct denials. In web discussions I found some derisions of askers as if it silly wish. Nowadays, when most languages introducing lambda expressions with multiline capability this looks strange. No, unfortunately this is not possible. 来源: https://stackoverflow.com/questions/29180642/multiline-anonymous-function-in-matlab