Hello,
I am currently working on a eventhandling system or something similar,
and have the problem of loosing scope. I have read many interesting
posts on this group and the faq article about closure, but it seems
that i have still not understood everything. Below is my attempt to
preserve the scope but it's not really nice and i think with the use
of closure could it be done better. But at the moment i am quite
confused and hope that someone can give me a hint...
Another problem is that different browser have different ways of
walking through a for-in loop, and so the functions are called in
different sequences. Any idea how the js-engine handles a for-in loop?
any ideas, comments, recommendations are welcome...
<html>
<head>
<script type="text/javascript">
HTMLElement = function (type) {
this.node = typeof type == "string"
? document.create Element(type) : type;
this.events = {};
};
HTMLElement.pro totype.initEven t = function(event) {
this.events[event] = {};
/*
self is a reference which gives us the ability
to access the object from the inner function below
*/
var self = this;
this[event] = function (e) {
e = e || window.event;
for (var x in self.events[event]) {
if (typeof self.events[event][x] == "function") {
self.events[event][x](e, this);
} else if (typeof self.events[event][x][x] == "function") {
self.events[event][x][x](e, this);
}
}
};
this.node[event] = this[event];
};
HTMLElement.pro totype.attachEv ent = function(event, obj, name) {
if (!this.events[event]) {
this.initEvent( event);
}
if (typeof obj == "object" && typeof name == "string") {
this.events[event][name] = obj;
} else if (typeof obj == "string" && typeof name == "function") {
this.events[event][obj] = name;
}
};
HTMLElement.pro totype.removeEv ent = function(event, name) {
if (this.events[event] && this.events[event][name]) {
delete this.events[event][name];
}
};
var myDemoClass = function (a, b) {
var self = this;
this.a = a;
this.b = b;
/* private function */
var alertAandB = function (){
alert(self.a+" "+self.b);
};
this.output = new HTMLElement(doc ument.body);
this.output.att achEvent("oncli ck", this, "alertA");
this.output.att achEvent("oncli ck", this, "alertB");
this.output.att achEvent("oncli ck", "alertAandB ", alertAandB);
};
/* 2 public methods */
myDemoClass.pro totype.alertA = function() {
alert(this.a);
};
myDemoClass.pro totype.alertB = function() {
alert(this.b);
};
this.onload = function() {
myDemoInstance = new myDemoClass("He llo", "World");
};
</script>
</head>
<body>
click me
</body>
</html>
I am currently working on a eventhandling system or something similar,
and have the problem of loosing scope. I have read many interesting
posts on this group and the faq article about closure, but it seems
that i have still not understood everything. Below is my attempt to
preserve the scope but it's not really nice and i think with the use
of closure could it be done better. But at the moment i am quite
confused and hope that someone can give me a hint...
Another problem is that different browser have different ways of
walking through a for-in loop, and so the functions are called in
different sequences. Any idea how the js-engine handles a for-in loop?
any ideas, comments, recommendations are welcome...
<html>
<head>
<script type="text/javascript">
HTMLElement = function (type) {
this.node = typeof type == "string"
? document.create Element(type) : type;
this.events = {};
};
HTMLElement.pro totype.initEven t = function(event) {
this.events[event] = {};
/*
self is a reference which gives us the ability
to access the object from the inner function below
*/
var self = this;
this[event] = function (e) {
e = e || window.event;
for (var x in self.events[event]) {
if (typeof self.events[event][x] == "function") {
self.events[event][x](e, this);
} else if (typeof self.events[event][x][x] == "function") {
self.events[event][x][x](e, this);
}
}
};
this.node[event] = this[event];
};
HTMLElement.pro totype.attachEv ent = function(event, obj, name) {
if (!this.events[event]) {
this.initEvent( event);
}
if (typeof obj == "object" && typeof name == "string") {
this.events[event][name] = obj;
} else if (typeof obj == "string" && typeof name == "function") {
this.events[event][obj] = name;
}
};
HTMLElement.pro totype.removeEv ent = function(event, name) {
if (this.events[event] && this.events[event][name]) {
delete this.events[event][name];
}
};
var myDemoClass = function (a, b) {
var self = this;
this.a = a;
this.b = b;
/* private function */
var alertAandB = function (){
alert(self.a+" "+self.b);
};
this.output = new HTMLElement(doc ument.body);
this.output.att achEvent("oncli ck", this, "alertA");
this.output.att achEvent("oncli ck", this, "alertB");
this.output.att achEvent("oncli ck", "alertAandB ", alertAandB);
};
/* 2 public methods */
myDemoClass.pro totype.alertA = function() {
alert(this.a);
};
myDemoClass.pro totype.alertB = function() {
alert(this.b);
};
this.onload = function() {
myDemoInstance = new myDemoClass("He llo", "World");
};
</script>
</head>
<body>
click me
</body>
</html>
Comment