473,472 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

this keyword and closures

I have a question about closures. Take for example the addBehavior
method below from the Low Pro library

Event.addBehavior = function(rules) {
var ab = this.addBehavior;
Object.extend(ab.rules, rules);

if (!ab.responderApplied) {
Ajax.Responders.register({
onComplete : function() {
if (Event.addBehavior.reassignAfterAjax)
setTimeout(function() { ab.reload() }, 10);
}
});
ab.responderApplied = true;
}

if (ab.autoTrigger) {
this.onReady(ab.load.bind(ab, rules));
}

};

var ab is initialized to this.addBehavior. It is then extended using
Object.extend(ab.rules.rules). However, in the Ajax.Responders
onComplete function, addBehavior is referenced using
Event.addBehavior.reassignAfterAjax instead of ab.reassignAfterAjax.
Then, immediately after, ab.reload is referenced. Why is this? Instead
of ab.rules, why wasn't
Object.extend(Event.addBehavior.rules, rules), or even
Object.extend(this.addBehavior.rules, rules)? Does it have to do with
closures?
Sep 19 '08 #1
1 1705
Jeff <jk**********@gmail.comwrites:
I have a question about closures. Take for example the addBehavior
method below from the Low Pro library
Never heard of that library, but ok.
Event.addBehavior = function(rules) {
var ab = this.addBehavior;
Object.extend(ab.rules, rules);

if (!ab.responderApplied) {
Ajax.Responders.register({
onComplete : function() {
if (Event.addBehavior.reassignAfterAjax)
setTimeout(function() { ab.reload() }, 10);
}
});
ab.responderApplied = true;
}

if (ab.autoTrigger) {
this.onReady(ab.load.bind(ab, rules));
}

};

var ab is initialized to this.addBehavior. It is then extended using
Object.extend(ab.rules.rules).
yes.
However, in the Ajax.Responders onComplete function, addBehavior is
referenced using Event.addBehavior.reassignAfterAjax instead of
ab.reassignAfterAjax.
It's not clear from the code that Event.addBehaviour.reassignAfterAjax
=== ab.reassignAfterAjax, though it probably is.
Then, immediately after, ab.reload is referenced. Why is this? Instead
of ab.rules, why wasn't
Object.extend(Event.addBehavior.rules, rules), or even
because it's shorter?
Object.extend(this.addBehavior.rules, rules)? Does it have to do with
closures?
since you can't close over this, yes, it has to do with closures. this
is NOT a variable.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Sep 19 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Alexander May | last post by:
When I define a function in the body of a loop, why doesn't the function "close" on the loop vairable? See example below. Thanks, Alex C:\Documents and Settings\Alexander May>python Python...
10
by: Emre Sevinc | last post by:
Take a look at the following snippet: <html> <head> <script> function add(elementId) { var container = document.getElementById(elementId); for (var i = 0; i < 10; i++) { var elt =...
5
by: sriram.nandakumar | last post by:
Hi there.. I have this piece of code to basically understand the event model in IE. <html> <head> <title>Events</title> <script> function callMe(){
16
by: Karl Kofnarson | last post by:
Hi, while writing my last program I came upon the problem of accessing a common local variable by a bunch of functions. I wanted to have a function which would, depending on some argument,...
7
by: Ray | last post by:
Hello, What do you think about emulating private variables for a class this way? function Something() { var private; Something.prototype.getPrivate = function() { return private; }...
4
by: Adam Risser | last post by:
Hi everyone, I am modifying the suckerfish dropdown code to use settimeout to have a slight pause before the menus disappear to make it more user friendly. I have hit a snag with the following...
5
by: Jeremy | last post by:
Is there any good reading about pitfalls of scoping when using events? Here is my specific issue: function MyType() { this.foo = "bar"; this.textbox = document.createElement("input");...
10
by: Stevo | last post by:
Simplified example code which can be copy/pasted and will exhibit the problem. var ob={id:2}; ob.alertid=function(){alert("id="+this.id);} ob.funcs=; ob.testalert=function(){ this.alertid();...
4
by: Peter | last post by:
Hi I've been delving into "delegates" and "anonymous methods", and now I've come across the term "closure". Some information I've found says that C# does not have closures, other information...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.