I think I have this figured out, but am hoping to cleary have this explained -- what precisely is the difference between these two javascript snippets?
;Person1 = (function() {
var FirstName = 'Hello';
var LastName = 'World';
this.sayHello = function() {
alert(FirstName + ' ' + LastName);
};
});
;Person2 = (function() {
...
I'm kind of new to Ruby and some of the closure logic has me a confused. Consider this code:
array = []
for i in (1..5)
array << lambda {j}
end
array.map{|f| f.call} # => [5, 5, 5, 5, 5]
This makes sense to me because i is bound outside the loop, so the same variable is captured by each trip through the loop. It also makes sense to ...
I have a code similar to this:
$.ajax({
success: function(data) {
text = '';
for (var i = 0; i< data.length; i++) {
text = text + '<a href="#" id="Data_'+ i +'">' + data[i].Name + "</a><br />";
}
$("#SomeId").html(text);
for (var i = 0; i< d...
Hi, I'm trying to simplify some JS code that uses closures but I am getting nowhere (probably because I'm not grokking closures)
I have some code that looks like this:
var server = http.createServer(function (request, response) {
var httpmethods = {
"GET": function() {
alert('GET')
},...
I've been looking at how some jQuery plugins work and I've seen the following acting as a closure around the whole plugin
$(function(){
// plugin code here
});
(function($){
// plugin code here
})(jQuery);
What is the difference between these two ?
...
I have read quite a few articles on closures, and, embarassingly enough, I still don't understand this concept! Articles explain how to create a closure with a few examples, but I don't see any point in paying much attention to them, as they largely look contrived examples. I am not saying all of them are contrived, just that the ones I ...
I am having trouble passing data retrieved from a $.post() function to use in other places in my code. I want to save the data as a variable and use it outside of the post() function. This is my code:
var last_update = function() {
$.post('/--/feed',
{func:'latest', who:$.defaults.login},
function($j){
_j = JSON.parse($j); ...
I searched but couldn't find an answer to this seemingly easy question, so...
Suppose I have a loop in which I need to set callbacks. My callback function looks like this:
function callback(var1) { // code }
Now my loop is something like this:
for( //condition)
{
var x = something_different_each_time;
document.getElementById('fo...
Hi,
i'm trying to play with php5.3 and closure.
I see here (Listing 7. Closure inside an object : http://www.ibm.com/developerworks/opensource/library/os-php-5.3new2/index.html) that it's possible to use $this in the callback function, but it's not. So I try to give $this as use variable :
$self = $this;
$foo = function() use($self) {...
Normally I'm well aware that a consideration like this is premature optimization. Right now I have some event handlers being attached inside a foreach loop. I am wondering if this style might be prone to leaks or inefficient memory use due to closures being created. Is there any validity to this thinking?
...
As far as I understand it, there is no way in Scala to have multiple return points in an anonymous function, i.e.
someList.map((i) => {
if (i%2 == 0) return i // the early return allows me to avoid the else clause
doMoreStuffAndReturnSomething(i) // thing of this being a few more ifs and returns
})
raises an error: return outs...
Can you give me an example, in PHP, that shows how closures are helpful in creating a DSL (fluent interface)?
edit:
The accepted answer in the following question tells about nested closures. If someone could translate that example to PHP that would be helpful too:
http://stackoverflow.com/questions/1966515/experience-with-fluent-interf...
I wrote the following code for a project that I'm working on:
var clicky_tracking = [
['related-searches', 'Related Searches'],
['related-stories', 'Related Stories'],
['more-videos', 'More Videos'],
['web-headlines', 'Publication']
];
for (var x = 0, length_x = clicky_tracking.length; x < length_x; x++) {
links = document.ge...
I'm still struggling with this concept. I have two different Person objects, very simply:
;Person1 = (function() {
function P (fname, lname) {
P.FirstName = fname;
P.LastName = lname;
return P;
}
P.FirstName = '';
P.LastName = '';
var prName = 'private';
P.showPrivate = function() { alert(...
I am trying to create and array of objects so that I can access them in a for loop in jQuery and I know that this works in Actionscript so what I am trying to do is convert my current knowledge to jQuery that will work.
Please have a look at this and tell me why I cannot access divToShow
Thanks guys
var homeImages = new Array();
homeI...
Hi,
I know that nested functions are not part of the standard C, but since they're present in gcc (and the fact that gcc is the only compiler i care about), i tend to use them quite often.
Is this a bad thing ? If so, could you show me some nasty examples ?
What's the status of nested functions in gcc ? Are they going to be removed ?
...
Hi. Code not working because of async query and variable scope problem. I can't understand how to solve this. Change to $.ajax method with async:false - not an option. I know about closures, but how I can implement it here - don't know. I've seen all topics here about closures in js and jQuery async problems - but still nothing. Help, pl...
I'm writing a parser in F#, and it needs to be as fast as possible (I'm hoping to parse a 100 MB file in less than a minute). As normal, it uses mutable variables to store the next available character and the next available token (i.e. both the lexer and the parser proper use one unit of lookahead).
My current partial implementation use...
I was hoping someone could explain to me what bad thing could happen in this code, which causes ReSharper to give an 'Access to modified closure' warning:
bool result = true;
foreach (string key in keys.TakeWhile(key => result))
{
result = result && ContainsKey(key);
}
return result;
Even if the code above seems safe, what bad t...
In other words, "A serialization framework for closures and their set of (IL-code) dependencies".
...