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.
...
jQuery.fn.testeee = function(_msg)
{
alert(_msg);
$(this[0]).overlay({
onBeforeLoad: function()
{
alert(_msg);
}
}).load();
};
$("#popup").testeee ('test');
$("#popup").testeee ('another_test');
This displays:
test
test
another_test
test
The alert() inside de anonymous function asigned to onBeforeLoad keeps showing "t...
To my knowledge, combined with the knowledge of others, among the mainstream languages
Objective C
C#
VB.net
Java
Python
Ruby
Javascript
Lisp
Perl
have closures and anonymous functions. Plain C/C++ doesn't have either of those.
Do closures in these languages have the same semantics? How important are they for everyday programming?
...
Is the recent movement towards anonymous methods/functions by mainstream languages like perl and C# something important, or a weird feature that violates OO principles?
Are recent libraries like the most recent version of Intel's Thread Building Blocks and Microsofts PPL and Linq that depend on such things a good thing, or not?
Are lan...
Let's say I get an anonymous function an need to act on its context, but it's different whether it's binded to "window" or an unknown object.
How do I get a reference to the object an anonymous function is called from?
EDIT, some code :
var ObjectFromOtherLibIAmNotSupposedToknowAbout = {
foo : function() {
// do something ...
I'm learning more about Scala and I'm having a little trouble understanding the example of anonymous functions here 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)...
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 v...
I have two functions that I want to run on different threads (because they're database stuff, and they're not needed immediately).
The functions are:
getTenantReciept_UnitTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciept_Unit);
getTenantReciept_TenantNameTableAdapter1.Fill(rentalEaseDataSet1.GetTenantReciep...
I'm learning Scala and I'm trying to store a function in a var to evaluate it later:
var action:() => Any = () => {}
def setAction(act: => Any) {
action = act
}
but that doesn't compile:
error: type mismatch;
found: Any
required: () => Any
action = act
So it seems to me that in action = act instead of assigning the fu...
I am using the following code to increment the elements in a 2d array surrounding a given element.
EmptyCell = {number: 0}; //This has several parts in the actual code.
list = new Array();
function init(w,h){
for (var x = 0; x <= w; x++){
list[x] = new Array();
for (var y = 0 ; y <= h; y++){
list[x][y]...
I've written a very simple GUI in MATLAB that will convert temperatures. It is meant to serve as a tutorial for a class of students. A strange thing has happened though. As with any MVC design pattern, there is a model object, a view object and a controller function. In order to set the output field of the GUI (the converted temperat...
Summary
Can you explain the reasoning behind the syntax for encapsulated anonymous functions in JavaScript? Why does this work: (function(){})(); but this doesn't: function(){}();?
What I know
In JavaScript, one creates a named function like this:
function twoPlusTwo(){
alert(2 + 2);
}
twoPlusTwo();
You can also create an ano...
In this SO thread, Brian Postow suggested a solution involving fake anonymous functions:
make a comp(L) function that returns the version of comp for arrays of length L... that way L becomes a parameter, not a global
How do I implement such a function?
...
If I have a chunk of code like this:
.hover(
function () {
hoverState($("#navbar a").index(this),1);
},
function () {
hoverState($("#navbar a").index(this),-1);
});
Is there any way to get rid of the anonymous functions and just say:
.hover(
hoverState($("#navbar a").index(this),1),
hoverState($("#n...
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 u...
When clicking on each div it should alert '1' if div 1 was clicked on or '5' if div 2 was clicked on. I have tried to make this code as easy to as possible because this is needed in a much larger application.
<html>
<head>
<style type="text/css">
#div1 { background-color: #00ff00; margin: 10px; padding: 10px; }
#div2 { background-color:...
After hearing the latest Stack Overflow podcast, Peter Norvig's compact Python spell-checker intrigued me, so I decided to implement it in Scala if I could express it well in the functional Scala idiom, and also to see how many lines of code it would take.
Here's the whole problem. (Let's not compare lines of code yet.)
(Two notes: You...
Hello all,
Typically, when needing to access an event, you do so via the parameter specified in the callback function:
$button.live("click", function(ev) {
// do something with ev here, like check 'ev.target'
}
But instead (for reasons too complicated to get into here), I do not want to use an anonymous callback function, but inste...
Let's say I have this method:
def myMethod(value:File,x: (a:File) => Unit) = {
// some processing here
// more processing
x(value)
}
I know I can call this as:
myMethod(new File("c:/"),(x:File) => println(x))
Is there a way I could call it using braces? Something like:
myMethod(new File("c:/"),{ (x:File) =>
if(x.toSt...
I use anonymous functions for diagnostic printing when debugging in MATLAB. E.g.,
debug_disp = @(str) disp(str);
debug_disp('Something is up.')
...
debug_disp = @(str) disp([]);
% diagnostics are now hidden
Using disp([]) as a "gobble" seems a bit dirty to me; is there a better option? The obvious (?) method doesn't work:
debug_disp ...