Is jQuery an implementation of the Decorator Design Pattern?
it decorates objects, so I th开发者_如何学运维ink it is, but I'm not sure.
example
jQuery(document).hide()
changes the document object by adding extra style.
EDIT:
If its not the Decorator Design Pattern? than what is it? There must be a pattern name for it!
It does not add new behaviour to an existing object. It just creates a new object that contains the existing objects as one of its values. It does mirror a lot of the functionality of the existing object through a different API and also allows public access to the existing object.
For it to be a proper decorated you should be able to access all methods and values of the DOM objects passed in without going through a different API or without having to access the underlying decorated object manually.
jQuery best fits the facade pattern, as it does "define a higher-level interface that makes the subsystem easier to use." For example, .css()
and .hide()
are features designed for ease of use, and so is jQuery's ability to perform an action to multiple elements at once:
$('.foo').css({left: '100px', top: '100px'}).hide(); // jQuery
// Pure JavaScript
for(var a = document.getElementsByClassName('foo'), i = 0; i < a.length; ++i) {
a[i].style.left = '100px';
a[i].style.top = '100px';
a[i].style.display = 'none';
}
jQuery only even seems to fit the decorator pattern in such ways as its animation functionality. Normal HTML DOM elements do not offer timed animations and attached queues, so jQuery adds that. In most other areas, jQuery provides the same functionality as available by accessing the underlying DOM elements directly.
But even there, it really doesn't fit, as jQuery is not "dynamically keeping the same interface."
I think you could call it like that because JQuery is able to grab html elemets by ids and decoracets them with javascript functions, ajax and other features.
No, not in the sense that you are thinking. jQuery plugins frequently use the decorator pattern, but the core library does not.
When you call a jQuery selector like $(document)
it is returning a new instance of the jQuery object, which only refers to the original document
object.
精彩评论