Is jQuery an implementation of the Decorator design pattern?

it decorates objects, so I think it is, but I'm not sure.

Example

jQuery(document).hide() 

Modifies the document object, adding an additional style.

EDIT:

If this is not a Decorator design template? what is this? For this there must be a template name!

+4
source share
4 answers

jQuery is best suited for faΓ§ade design , as it "defines a higher-level interface that makes using subsystems easier." For example, .css() and .hide() are functions designed for ease of use, as well as the ability of jQuery to perform an action on several 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 seems to even match the decorator pattern in such a way as its animated functionality. Normal HTML DOM elements do not offer time-match animation and attached queues, so jQuery adds this. In most other areas, jQuery provides the same functionality that is available with direct access to the underlying DOM elements.

But even there, it really doesn't work, since jQuery does not "dynamically support the same interface."

+1
source

It does not add new behavior to an existing object. It simply creates a new object that contains existing objects as one of its values. It reflects many of the functions of an existing object using another API, and also allows public access to an existing object.

For it to be properly designed, you must have access to all the methods and values ​​of DOM objects passed without passing another API or without having to access the main decorated object manually.

+4
source

I think you could call it that because JQuery can capture html elemets with identifiers and decorates them with javascript, ajax and other functions.

0
source

No, not in the sense that you are thinking. JQuery plugins often use a decorator pattern, but not in the main library.

When you call a jQuery selector of type $(document) , it returns a new instance of the jQuery object, which applies only to the original document object.

0
source

Source: https://habr.com/ru/post/1336934/


All Articles