closures

JavaScript OOPS Question

Hey guys, A JavaScript newbie here. I have this following code: function testObject(elem) { this.test = "hi"; this.val = elem; console.log(this.test+this.val); echo(); function echo () { console.log(this.test+this.val); } } var obj = new testObject("hello"); When it is run, I expect "hihello" to be ...

Simulate AJAX latency with a wrapping closure

I'd like to wrap Prototype Ajax.Request in order to simulate AJAX latency. I mean, using a closure and Prototype's delay() facility, but apparently there is something wrong with my code /* * Purpose: simulate AJAX latency when developing on localhost * What's wrong? */ Ajax.Request = (function(original) { return function(url, optio...

Complexity of Java 7's current Lambda proposal? (August 2010)

Some people say that every programming language has its "complexity budget" which it can use to accomplish its purpose. But if the complexity budget is depleted, every minor change becomes increasingly complicated and hard to implement in a backward-compatible way. After reading the current provisional syntax for Lambda (≙ Lambda expres...

Why does this Javascript object not go out of scope after $(document).ready?

I have some working Javascript that manipulates the some DOM elements. The problem is, I don't understand why it works, which is never a good thing. I am trying to learn more about object oriented javascript and javascript best practices, so the organization may seems a little strange. Basically, I wrap two methods that manipulate the ...

How to keep variable in scope

I have an associative array of objects that I want to extend with a number of functions. myCtrls //Array of objects I do this with the following loop $(document).ready(function() { for (ctrlName in fieldCtrls){ var ctrl = fieldCtrls[ctrlName]; ctrl.Initialize = function(){ //Do some stuff ...

I know what a closure is, but I still dont understand why (or when) you would use them

My understanding of closures is that they are essentially a function which uses a variable that you would assume would be out of scope. I guess heres an example I saw the other day: function closureMaker(somearg) { var local_value = 7; function funcToReturn(arg1, arg2) { return local_value + somearg + arg1 + arg2; ...

Problem with LINQ, anonymous types, and closures

I have a piece of code that filters a list using LINQ, creates a list of instances of an anonymous type, and assigns an event handler to each instance: // Select every linear expression and create a menu item from it var items = from expr in expressionList.Expressions where expr.Type == ExpressionType.Linear let ...

How can I seperate objects that is under same closure into different files.

Hi, I have following structure for my client; var myObject = (function(){ var mainObjectList = []; var globalObject = { init:function(mainObjectId){ var logger = {}; var utilityObject1 = {}; var utilityObject2 = {}; var mainObject = {}; m...

JavaScript img onLoad and closures - why does this code trigger an infinite loop?

I have some Google Maps JS that plots a number of markers on a map. However, when loading the markers I noticed that the images were not loading in time and so the markers were not being placed. To get around this I modified my function that returned a map marker as follows: function newGoogleMapPin(type){ var imgpath = "img/gmapico...

How to dynamically define a class method which will refer to a local variable outside?

class C end var = "I am a local var outside" C.class_eval do def self.a_class_method puts var end # I know, this is not correct, because the 'def' created a new scope; # I am asking a solution to make it; # I also know that use 'define_method' can create a instance method without creating a new scope. # but my...

A bunch of little closures vs one big one

I have a Google Map, and I am adding event listeners to different things. For instance, I have a Point object, and for this object, I have been adding events as thus: google.maps.event.addListener(this.marker, 'click', (function(point) { return function(event) { alert(point.index); }})(this)); I have a lot of these ev...

Is a Foreach loop a type of block?

In thinking about blocks, I've always wondered why the thingers.each { |thing| example is actually interesting (since there's another, built-in way to do it). In most modern languages there's a way to iterate through a collection and apply some inline code to it. But then I thought that maybe the for (Thing thing : things) { syntax is...

Passing parameters in Javascript onClick event

Hi I'm trying to pass a parameter in the onclick event. Below is a sample code: <div id="div"></div> <script language="javascript" type="text/javascript"> var div = document.getElementById('div'); for (var i = 0; i < 10; i++) { var link = document.createElement('a'); link.setAttribute('href', '#'); link.inn...

How to Get A JavaScript Variable Value from Inside a Function?

I'm using the JQuery getJSON function to get some JSON-formatted data. The data will be used along with the YouTube JavaScript API: http://code.google.com/apis/youtube/js_api_reference.html The JSON data is not pulled from YouTube. It's a simple, hand-written list of YouTube Video ID's and Titles which are listed on a CSV. I'm using PH...

best known transitive closure algorithm for graph

In terms of runtime, what is the best known transitive closure algorithm for directed graphs? I am currently using Warshall's algorithm but its O(n^3). Although, due to the graph representation my implementation does slightly better (instead of checking all edges, it only checks all out going edges). Is there any transitive closure algo...

How to evaluate a variable so that it is treated like its contents in JavaScript?

Ok, I don't know how to actually ask this question without showing it. (Also explains why I can't figure out how to search for it on Google.) Given the following like of code: dijit.byId('leftNavigationPane').onLoad = function(){setSecondaryNav(url.secondaryNavId);}; I want the variable url.secondaryNavId to be evaluated, so what I ...

Binding click event handlers in a loop causing problems in jQuery

I am trying to run the following code: I pass parameter to a function, but it always has the value of the last object run through the loop. I read some articles about it on stackoverflow, but I couldn't find out how to make it run in my solution. The object is a JSON object returned from the server. All it's values are correct. for(v...

Is it possible to use closures in AS2 like in Javascript?

I am wondering if it is possible to create a closure in ActionScript2 like it is possible in Javascript. This doesn't work: var util = function(){ var init = function(){ trace(this + ': util'); // i want to know this thing works! var myUtils = new MyAS2Utils(); // load some stuff var url = myUtils.getURLInSomeReall...

defineSetter in a loop

As a followup to http://stackoverflow.com/questions/3558240 I'm now having troubles with defining setters in a loop. This is the code that I'm attempting to use within a Greasemonkey script. var setValue_InStoredObject = function(_prefName, _varName, _newValue) { console.info('setValue_InStoredObject:\narguments = '); console.inf...

Can I "extend" a closure-defined "class" in Javascript?

I have a Javascript "class" defined like so: var Welcomer = function(name) { var pName = name; var pMessage = function() { return "Hi, " + pName + "!"; }; return { sayHi: function() { alert(pMessage()); } }; }; new Welcomer('Sue').sayHi(); Is there a way to "subclass" Welcomer in such a way that I can red...