anonymous-function

How can I pass a reference to a function, with parameters? [duplicate]

泄露秘密 提交于 2019-11-26 15:04:08
问题 Possible Duplicate: How can I pre-set arguments in JavaScript function call? (Partial Function Application) I need to able to pass a reference to a function with a given set of parameters . Here is an example of passing a reference without parameters: var f = function () { //Some logic here... }; var fr = f; //Here I am passing a reference to function 'f', without parameters fr(); //the 'f' function is invoked, without parameters Now what I need to do is pass the same f function, but this

How to execute multiple statements in a MATLAB anonymous function?

吃可爱长大的小学妹 提交于 2019-11-26 14:31:58
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. 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 = @()

Skipping outputs with anonymous function in MATLAB

久未见 提交于 2019-11-26 14:28:19
问题 Say I want to create an anonymous function from a m-file-function that returns two outputs. Is it possible to set up the anonymous function such that it only returns the second output from the m-file-function? Example: ttest2 returns two outputs, t/f and a probability. If I want to use the t-test with cellfun , I might only be interested in collecting the probabilities, i.e. I'd like to write something like this probabilities = cellfun(@(u,v)ttest2(u,v)%take only second output%,cellArray1

Javascript: closure of loop?

两盒软妹~` 提交于 2019-11-26 13:47:48
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? 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 make functions within a loop.". Live demo: http://jsfiddle.net/simevidas/ZKeXX/ One solution, if you're coding for a

Javascript anonymous function call [duplicate]

家住魔仙堡 提交于 2019-11-26 12:41:11
问题 This question already has an answer here: What does the exclamation mark do before the function? 10 answers I was reading JS sources from Twitter — on my way to improve my JS knowledge base, when I came across the strange way of calling anonymous function: !function( $ ) { ... }( window.jQuery ); ... and this works! :) It\'s obvious to everyone, that this: function ( $ ) { ... } ( window.jQuery ) does not work (Syntax error), while this one is correct: (function ( $ ) { .... })( window.jQuery

(…()) vs. (…)() in javascript closures [duplicate]

那年仲夏 提交于 2019-11-26 11:14:39
This question already has an answer here: Location of parenthesis for auto-executing anonymous JavaScript functions? 4 answers I know this is silly, but there's any difference between this: (function() { var foo = 'bar'; })(); and this? (function() { var foo = 'bar'; }()); JSLint tells us to Move the invocation into the parens that contain the function , but I see no need to. Edit: The answers are too cool. ~function , the JSHint alternative along with jQuery's preference for (/***/)(); and Crockford's explanation! I thought I was going to just get a "they're the same thing" kind of answer.

How to removeEventListener that is addEventListener with anonymous function?

江枫思渺然 提交于 2019-11-26 11:13:43
function doSomethingWith(param) { document.body.addEventListener( 'scroll', function() { document.write(param); }, false ); // An event that I want to remove later } setTimeout( function() { document.body.removeEventListener('scroll', HANDLER ,false); // What HANDLER should I specify to remove the anonymous handler above? }, 3000 ); doSomethingWith('Test. '); You can't. You have to use a named function or store the reference somehow. var handler; function doSomethingWith(param) { handler = function(){ document.write(param); }; document.body.addEventListener('scroll', handler,false); }

How does a function in a loop (which returns another function) work? [duplicate]

妖精的绣舞 提交于 2019-11-26 10:23:54
This question already has an answer here: JavaScript closure inside loops – simple practical example 43 answers I've been trying to assign a function to onclick event of a dynamically created "a" tag in JavaScript. All of the tags are created in a loop as follows: for ( var i = 0; i < 4; i++ ) { var a = document.createElement( "a" ); a.onclick = function( ) { alert( i ) }; document.getElementById( "foo" ).appendChild( a ); } The alerted value for all four links is always "4". Pretty obvious. When googling I came across a post that shows the following code snippet: a.onclick = (function(p, d) {

Why is “this” in an anonymous function undefined when using strict?

99封情书 提交于 2019-11-26 10:21:05
Why is this in an anonymous function undefined when using javascript in strict mode? I understand why this could make sense, but I couldn't find any concrete answer. Example: (function () { "use strict"; this.foo = "bar"; // *this* is undefined, why? }()); Test in a fiddle: http://jsfiddle.net/Pyr5g/1/ Check out the logger (firebug). It's because, until ECMAscript 262 edition 5, there was a big confusion if people who where using the constructor pattern , forgot to use the new keyword. If you forgot to use new when calling a constructor function in ES3, this referenced the global object (

How to pass two anonymous functions as arguments in CoffeScript?

对着背影说爱祢 提交于 2019-11-26 09:31:41
问题 I want to pass two anonymous functions as arguments for jQuery\'s hover, like so: $(\'element\').hover( function() { // do stuff on mouseover }, function() { // do stuff on mouseout } ); It\'s easy with just one – hover -> – but what is the proper syntax in CoffeeScript for two? I tried ...hover -> , ...hover( ->... , etc. but nothing gets me the above structure. 回答1: Put parentheses around the anonymous functions. 回答2: I think the problem lies with using single line comments // . Single-line