On Jan 26, 4:51 am, "gg9h0st" <mn9h...@hotmail.comwrote:
i saw a code refactorying onload event listener
window.onloadListeners=new Array();
window.addOnLoadListener=function(listener) {
window.onloadListeners[window.onloadListeners.length]=listener;
}why declare the onloadListeners, addOnLoadListener below window?
i think bare "afunc" and "window.afunc" are same thing and available
both way.
is there a difference?
My saying about this matter is that, overloading the window object
with global properties is plain stupid. Even if window.aFunc is the
same as var aFunc (or function aFunc), the point is not really there ;
any implementation should be placed in their own namespace (or class).
For example, an event handler system should be implemented in a class
Object like Event, then have all properties and functions attached
directly to it.
Of course, Event would be a property of the window object, but that
ressemblance would stop there, as other attributes and properties
would all be in a specific classification. (Hence the name 'class'.)
Javascript might be an interpreted language, but it is also an object
oriented one ; programmers should think like so and stop writing
programs like if it were procedural (like BASIC).
This technique not only ensure having a better readable code (where
everything is not piled up at the same place, but in a human
comprehensive way, or hierarchy), but it also ensure expandable code
that will not break down due tu variable / function conflicts with
other third party scripts or libraries. Even if it's only for a small
project, reusable code concept save time, creates good coding habbits,
make the code well structured, etc.
Now, to come back to the original matter, you should read about
"variable context" in Javascript ; you'll notice that "var a" is not
always the same thing as "this.a", or in other words, "var
onloadListeners=new Array();" is not always the same thing as
"window.onloadListeners=new Array();" (I would personally prefer this
syntax : "window.onloadListeners=[];"). (see
http://devedge-
temp.mozilla.org/library/manuals/2000/javascript/1.5/reference/
stmt.html#1066604).
If you are outside a function (or class), "var a = 1;" is the same as
"a = 1;" (hence the same as "window.a = 1;").
Inside a function, "var a = 1;" is called a local variable. Executing
only "a = 1;" inside a function will throw a warning saying that "a"
hasn't been declared. Unlike Java, accessing a class variable requires
the "this" keyword".
Here's an example script that illustrate the fonctionality :
var test = 1;
alert( test ); // 1
window.test++;
alert( test ); // 2
this.test++;
alert( test ); // 3
// this is the same as var Test = function() ...
// or this.Test = function() ...
function Test() {
this.test = 100; // class variable
}
Test.prototype = {
// this is the same as Test.prototype.printTest =
function()...
printTest: function() {
alert( test ); // undefined
test++;
alert( test ); // NaN (should be accessed with this.test)
var test = 1; // local variable
alert( test ); // 1
test++;
alert( test ); // 2
this.test++;
alert( this.test ); // 101
}
};
var test = new Test();
test.printTest();
So, I hope that you see the idea behind having your functions and
methods well structured. Please code responsibly :)
-Yanick