Brian Feliciano wrote:
while YUI does something like this:
var ObjectName = function () {
return {
propertyOne: propertyValue,
methodOne: function (argv1,argv2) {
//do something here...
}
}
}();
they both seem to work fine, but are there any difference? performace wise?
I have not worked with YUI (prototype has been more than adequate for me
so I have not investigated others) but the main difference I see with
the above code is that it allows for private variables/methods to be
easily defined. Consider the following modification on the above code:
var ObjectName = function () {
var myPrivateVariable = 'secret';
function myPrivateMethod(arg1, arg2) {
// ... do something private ...
}
return {
propertyOne: propertyValue,
methodOne: function (argv1,argv2) {
alert(myPrivateVariable);
myPrivateMethod(argv1, argv2);
}
}
}();
In this example we have a private variable and a private method that
cannot be accessed from outside of "ObjectName" but are perfectly
accessible inside "ObjectName" because of the closure. May cause memory
leaks if you reference host objects though... Would have to be careful
about that.
Prototype takes the "advisory" approach to making methods private. For
example the collection objects (Hash, Array, RangeObject) all define a
"_each" method that is used by the Enumerable mixin for iteration. This
"_each" method is private (or maybe it would be considered protected?)
but it does not enforce this restriction. You know it is internal
because of the "_". You can obey that restriction (by using the "each"
method which is meant for public use) or you can ignore the restriction
and use "_each" directly (perhaps you want a performance benefit by
avoiding the try...catch expression since you may not be using the
$break and $continue exceptions anyway).
Some people prefer enforcing member protection, others prefer the
advisory method. My guess is that Prototype takes the advisory approach
because of it's ancestry in dynamic languages like Ruby and Perl which
also tend to use the advisory method. I think the idea is to encourage
people to not shoot themselves in the foot but allow them to do so if
they really want since ideally the user of the library knows more about
their own domain than the developer of the library.
Eric
_______________________________________________
Rails-spinoffs mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs