Design Patterns used in the jQuery library

后端 未结 4 2004
臣服心动
臣服心动 2020-12-07 07:10

jQuery is highly focused on the DOM and provides a nice abstraction around it. In doing so, it makes use of various well known design patterns which just hit me yesterday. O

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 07:24

    The Composite pattern is also very commonly used in jQuery. Having worked with other libraries, I can see how this pattern is not so obvious as it looks at first sight. The pattern basically says that,

    a group of objects are to be treated in the same way as a single instance of an object.

    For example, when dealing with a single DOM element or a group of DOM elements, both can be treated in a uniform manner.

    $('#someDiv').addClass('green'); // a single DOM element
    
    $('div').addClass('green');      // a collection of DOM elements
    

提交回复
热议问题