anonymous-function

Dollar sign before self declaring anonymous function in JavaScript?

こ雲淡風輕ζ 提交于 2019-11-26 09:28:54
问题 What is the difference between these two: $(function () { // do stuff }); AND (function () { // do stuff })(); 回答1: The first uses jQuery to bind a function to the document.ready event. The second declares and immediately executes a function. 回答2: $(function() {}); is a jQuery shortcut for $(document).ready(function() { /* Handler for .ready() called. */ }); While (function() {})(); is a instantly invoked function expression, or IIFE. This means that its an expression (not a statement) and it

Anonymous recursive PHP functions

ぐ巨炮叔叔 提交于 2019-11-26 08:57:47
问题 Is it possible to have a PHP function that is both recursive and anonymous? This is my attempt to get it to work, but it doesn\'t pass in the function name. $factorial = function( $n ) use ( $factorial ) { if( $n <= 1 ) return 1; return $factorial( $n - 1 ) * $n; }; print $factorial( 5 ); I\'m also aware that this is a bad way to implement factorial, it\'s just an example. 回答1: In order for it to work, you need to pass $factorial as a reference $factorial = function( $n ) use ( &$factorial )

Returning anonymous functions from lapply - what is going wrong?

我的未来我决定 提交于 2019-11-26 08:25:25
问题 When trying to create a list of similar functions using lapply , I find that all the functions in the list are identical and equal to what the final element should be. Consider the following: pow <- function(x,y) x^y pl <- lapply(1:3,function(y) function(x) pow(x,y)) pl [[1]] function (x) pow(x, y) <environment: 0x09ccd5f8> [[2]] function (x) pow(x, y) <environment: 0x09ccd6bc> [[3]] function (x) pow(x, y) <environment: 0x09ccd780> When you try to evaluate these functions you get identical

Scala underscore minimal function

浪尽此生 提交于 2019-11-26 07:47:07
问题 Let\'s create a value for the sake of this question: val a = 1 :: Nil now, I can demonstrate that the anonymous functions can be written in shorthand form like this: a.map(_*2) is it possible to write a shorthand of this function?: a.map((x) => x) my solution doesn\'t work: a.map(_) 回答1: Your first shorthand form can also be written point-free a map (2*) Thanks to multiplication being commutative. As for (x) => x , you want the identity function. This is defined in Predef and is generic, so

javascript: recursive anonymous function?

十年热恋 提交于 2019-11-26 06:58:56
问题 Let\'s say I have a basic recursive function: function recur(data) { data = data+1; var nothing = function() { recur(data); } nothing(); } How could I do this if I have an anonymous function such as... (function(data){ data = data+1; var nothing = function() { //Something here that calls the function? } nothing(); })(); I\'d like a way to call the function that called this function... I\'ve seen scripts somewhere (I can\'t remember where) that can tell you the name of a function called, but I

How to call a closure that is a class variable?

主宰稳场 提交于 2019-11-26 06:58:16
问题 class MyClass { var $lambda; function __construct() { $this->lambda = function() {echo \'hello world\';}; // no errors here, so I assume that this is legal } } $myInstance = new MyClass(); $myInstance->lambda(); //Fatal error: Call to undefined method MyClass::lambda() So what is the correct syntax for reaching class variables ? 回答1: In PHP, methods and properties are in a separate namespace (you can have a method and a property with the same name), and whether you are accessing a property or

removeEventListener on anonymous functions in JavaScript

房东的猫 提交于 2019-11-26 05:26:11
问题 I have an object that has methods in it. These methods are put into the object inside an anonymous function. It looks like this: var t = {}; window.document.addEventListener(\"keydown\", function(e) { t.scroll = function(x, y) { window.scrollBy(x, y); }; t.scrollTo = function(x, y) { window.scrollTo(x, y); }; }); (there is a lot more code, but this is enough to show the problem) Now I want to stop the event listener in some cases. Therefore I am trying to do a removeEventListener but I can\'t

Reason behind this self invoking anonymous function variant

浪尽此生 提交于 2019-11-26 04:42:44
问题 While looking at code on github, I found the following: (function() { }).call(this); This is clearly a self invoking anonymous function. But why is it written this way? I\'m used to seeing the canonical variant (function() {})() . Is there any particular advantage to using .call(this) for a self invoking anonymous function? Edit: It looks like some commonjs environments set this to a non-global value at the top level of a module. Which ones, and what do they set this to that you might want to

How to execute multiple statements in a MATLAB anonymous function?

耗尽温柔 提交于 2019-11-26 03:56:33
问题 I\'d like to do something like this: >> foo = @() functionCall1() functionCall2() So that when I said: >> foo() It would execute functionCall1() and then execute functionCall2() . (I feel that I need something like the C , operator) EDIT: functionCall1 and functionCall2 are not necessarily functions that return values. 回答1: Trying to do everything via the command line without saving functions in m-files may be a complicated and messy endeavor, but here's one way I came up with... First, make

Javascript: closure of loop?

ぃ、小莉子 提交于 2019-11-26 03:44:47
问题 I would like to do the something along the following: for (var i = 0; i < 10; ++i) { createButton(x, y, function() { alert(\"button \" + i + \" pressed\"); } } The problem with this is that I always get the final value of i because Javascript\'s closure is not by-value. So how can I do this with javascript? 回答1: for(var i = 0; i < 10; i++) { (function(i) { createButton(function() { alert("button " + i + " pressed"); }); })(i); } Note that JSLint doesn't like this pattern. It throws "Don't