J
James
I am learning HTML/JavaScript... Okay...
Let me start out with some code... The following describes a very simple
"MyImage" object that has but a single "public" method "load(src, alt)".
However, it has two internal methods "prvOnLoad(e)/prvOnError(e)" that
belong to an internal object "this.image" in a sense: Once a user calls
`MyImage.load(src, alt)', the object will setup internal listeners via
`MyImage.prvAttachEventListeners()'. In addition, once an event fires, it
"disables" further events by setting `this.image.onload/error/abort'
callbacks to null:
// MyImage.js
-------------
function MyImage(something)
{
this.image = new Image();
this.something = something;
}
MyImage.prototype.prvAttachEventListeners =
function()
{
var this_ = this;
this.image.onload = function(e) { this_.prvOnLoad(e); };
this.image.onerror = function(e) { this_.prvOnError(e); };
this.image.onabort = function(e) { this_.prvOnError(e); };
}
MyImage.prototype.prvRemoveEventListeners =
function()
{
this.image.onload = null;
this.image.onerror = null;
this.image.onabort = null;
}
MyImage.prototype.prvOnLoad =
function(e)
{
alert(this + ":rvOnLoad() - says:" + this.something);
this.prvRemoveEventListeners();
}
MyImage.prototype.prvOnError =
function(e)
{
alert(this + ":rvOnError() - says:" + this.something);
this.prvRemoveEventListeners();
}
MyImage.prototype.load =
function(src,
alt)
{
this.prvAttachEventListeners();
this.image.alt = alt;
this.image.src = src;
}
-------------
Here is how one can use it:
-------------
function foo()
{
var i = new MyImage("Hello World: Images!");
i.load("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png", "alt
text 1");
}
-------------
My focus is on the "private" method
`MyImage.prototype.prvAttachEventListeners'... Will create a closure that
leaks `this'?
Let me start out with some code... The following describes a very simple
"MyImage" object that has but a single "public" method "load(src, alt)".
However, it has two internal methods "prvOnLoad(e)/prvOnError(e)" that
belong to an internal object "this.image" in a sense: Once a user calls
`MyImage.load(src, alt)', the object will setup internal listeners via
`MyImage.prvAttachEventListeners()'. In addition, once an event fires, it
"disables" further events by setting `this.image.onload/error/abort'
callbacks to null:
// MyImage.js
-------------
function MyImage(something)
{
this.image = new Image();
this.something = something;
}
MyImage.prototype.prvAttachEventListeners =
function()
{
var this_ = this;
this.image.onload = function(e) { this_.prvOnLoad(e); };
this.image.onerror = function(e) { this_.prvOnError(e); };
this.image.onabort = function(e) { this_.prvOnError(e); };
}
MyImage.prototype.prvRemoveEventListeners =
function()
{
this.image.onload = null;
this.image.onerror = null;
this.image.onabort = null;
}
MyImage.prototype.prvOnLoad =
function(e)
{
alert(this + ":rvOnLoad() - says:" + this.something);
this.prvRemoveEventListeners();
}
MyImage.prototype.prvOnError =
function(e)
{
alert(this + ":rvOnError() - says:" + this.something);
this.prvRemoveEventListeners();
}
MyImage.prototype.load =
function(src,
alt)
{
this.prvAttachEventListeners();
this.image.alt = alt;
this.image.src = src;
}
-------------
Here is how one can use it:
-------------
function foo()
{
var i = new MyImage("Hello World: Images!");
i.load("http://www.google.com/intl/en_ALL/images/srpr/logo1w.png", "alt
text 1");
}
-------------
My focus is on the "private" method
`MyImage.prototype.prvAttachEventListeners'... Will create a closure that
leaks `this'?