MooTools has a philosophy that any method that is defined in JS be used whenever possible and, when that method is insufficient, that a new method be used that replaces it. So, MooTools has setProperty that adds cross-browser support and other conveniences for setAttribute.
Element.set, on the other hand, is just a shortcut method for all the others. You can use set to set styles (element.setStyles), events, element.addEvents), properties/attributes (element.setProperties), etc. So set is kind of an overloaded method with a lot of functionality, but set must itself call something to assign these values. Rather than put the cross-browser conveniences inside set, MooTools has stand alone methods for all of set's functionality (well, most of it - not the custom setters/getters those *have* to use set). Thus:
set -> setStyle/setStyles
set -> addEvent/addEvents
set -> setProperty/setProperties
set wraps up all these methods into a single interface, but MooTools exposes the logic that set uses in methods of their own for greater extensibility and access.
I rarely use .set to setStyles - I just use setStyles because on its own it doesn't save may bites. I do use set for setProperties. Consider:
el.setStyles({ display: 'block', border: 'none' });
el.set({ styles: { display: 'block', border: 'none' } }); //harder to type out
el.setProperties({ src: '/foo.jpg', alt: 'foo' });
el.set({ src: '/foo.jpg', alt: 'foo' }); //easier than above
I *do* use set when I want to set numerous things:
el.set({
src: '/foo.jpg',
alt: 'foo',
styles: { display: 'block', border: 'none' },
events: {etc...}
});
I also use it when I create new elements:
new Element('img', {
src: '/foo.jpg',
alt: 'foo',
styles: { display: 'block', border: 'none' },
events: {etc...}
});
When you create an element and pass a second argument it's just passed along to .set.
So set is a convenience wrapper, but the methods set uses are exposed for your use if you care to reference them directly.
I think I just about "get" it (sorry... I'm awful) so:
"set" is a wrapper for
"setProperty" which is a wrapper for
"setAttribute"
However, if "set" should always be used, why is "setProperty" a public
method?
Michal.
> My bad, I confused myself.
> In my post previously I meant to be talking about setProperty. setAttribute
> is a native method:
>
>
https://developer.mozilla.org/en/DOM/element.setAttribute>
> setProperty is MooTools' wrapper for that method that helps you manage
> removing properties (setting them to null) as well as ensuring that
> properties that should be booleans (like checked) are set propertly.
>
> So set and setProperty are both used to set attributes, but set has
> additional functionality and should always be used.
>
> On Mon, Dec 29, 2008 at 10:57 AM, Michal-2 (via Nabble) <
View this message in context:
Re: get/set vs. store/retrieve