I'm trying to create a simple JavaScript object from scratch for learning purposes. I have to use YUI for business reasons (existing code base).
Are there any benefits of using YUI3 (latest) to create your JavaScript object?
Currently, i'm noticing that each public method i need to wrap it with a YUI instance and it seems very repetitive like so:
MYNAMESPACE.myobject = (function (Y) {
return {
create: function () {
Y.use('node', 'attribute','event', function () {
// Do my magic
}
},
update: function () {
Y.use('node', 'attribute', function () {
// Do my magic
});
}
}
//...
})(YUInstance);
Update:
My intention for writing it this way because i'm more comfortable writing this particular this augmented module pattern to instantiate my module singleton container:
MYNAMESPACE.myobject.create(configuration);
MYNAMESPACE.myobject.update();
MYNAMESPACE.myobject.init();
I would not compare plain JavaScript objects to YUI Modules. YUI modules serves many specific purposes which plain JS objects do not. Plain JS objects are the building blocks of JS, meaning the comparison you make is almost like asking what's better, atoms or proteins. Neither is better! There are no proteins without atoms, as there are no YUI modules without plain JS objects.
YUI modules allow developers to build modular, complex systems which is hard to do in JS today. This is due to the fact that JS lacks support for modules. A side note: support is planned and on the way.
This is not possible in JS, like it is possible in other languages (e.g. Python, Java):
import 'mymodule';
YUI adds support for something like it:
YUI().use('mymodule');
Which is one of the reasons why you would use YUI in a business context. YUI allows you to add modules, use modules, depend on modules, build large groups of modules and so on.
Also, your code example strikes me as unconventional. Normally one would wrap a module in a single use
callback, not two different ones, like so:
YUInstance.use('node', 'attribute','event', function (Y) {
Y.namespace('MYNAMESPACE').myobject = …
});