Anonymous recursive PHP functions
In order for it to work, you need to pass $factorial as a reference $factorial = function( $n ) use ( &$factorial ) { if( $n == 1 ) return 1; return $factorial( $n – 1 ) * $n; }; print $factorial( 5 );
In order for it to work, you need to pass $factorial as a reference $factorial = function( $n ) use ( &$factorial ) { if( $n == 1 ) return 1; return $factorial( $n – 1 ) * $n; }; print $factorial( 5 );
One solution, if you’re coding for a browser that uses JavaScript 1.7 or higher, is to use the let keyword: for(var i = 0; i < 10; ++i) { let index = i; createButton(x, y, function() { alert(“button ” + index + ” pressed”); } } From the MDC Doc Center: The let keyword causes … Read more
There is no practical difference in those two forms, but from a grammatical point of view the difference between the two is that The Grouping Operator – the parentheses – will hold in the first example a CallExpression, that includes the FunctionExpression: CallExpression | | FunctionExpression | | | V V (function() { }()); ^ … Read more
You can give the function a name, even when you’re creating the function as a value and not a “function declaration” statement. In other words: (function foo() { foo(); })(); is a stack-blowing recursive function. Now, that said, you probably don’t may not want to do this in general because there are some weird problems … Read more
You’re immediately calling an anonymus function with a specific parameter. An example: (function(name){ alert(name); })(‘peter’) This alerts “peter“. In the case of jQuery you might pass jQuery as a parameter and use $ in your function. So you can still use jQuery in noConflict-mode but use the handy $: jQuery.noConflict() (function($){ var obj = $(‘<div/>’, … Read more
By default, invoking a function like (function(){/*…*/})() will set the value of this in the function to window (in a browser) irrespective of whatever the value of this may be in the enclosing context where the function was created. Using call allows you to manually set the value of this to whatever you want. In … Read more
Arrow functions don’t have this since the arguments array-like object was a workaround to begin with, which ES6 has solved with a rest parameter: var bar = (…arguments) => console.log(arguments); arguments is by no means reserved here but just chosen. You can call it whatever you’d like and it can be combined with normal parameters: … Read more
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 your anonymous functions and put their handles in a cell array: fcn1 = @() …; fcn2 = @() …; fcn3 = @() …; fcnArray = … Read more
There’s no difference. Both are valid ways to get the JavaScript parser to treat your function as an expression instead of a declaration. Note that + and ! will also work, and are sometimes used by minifiers to save a character of size: +function() { var foo = ‘bar’; }(); !function() { var foo = … Read more
When you assign the function to the click handler, a closure is created. Basically a closure is formed when you nest functions, inner functions can refer to the variables present in their outer enclosing functions even after their parent functions have already executed. At the time that the click event is executed, the handler refers … Read more