jQuery库中使用的设计模式[英] Design Patterns used in the jQuery library

本文是小编为大家收集整理的关于jQuery库中使用的设计模式的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

jQuery高度专注于DOM,并在其周围提供了一个很好的抽象.为此,它利用了各种知名设计模式昨天刚刚打击了我.一个明显的例子是 decorator 模式. jQuery对象围绕常规DOM对象提供新的和附加功能.

例如,DOM具有本机 insertbefore 方法但是没有相应的insertafter方法.有各种实现可用 jQuery就是一个提供此功能的库:

$(selector).after(..)
$(selector).insertAfter(..)

在jQuery中大量使用了许多其他示例.

您还注意到图书馆本身的一部分,其他哪些大小的设计模式?另外,请提供模式使用的示例.

使它成为一个社区Wiki,因为我相信人们喜欢jQuery的各种事情可以追溯到众所周知的设计模式,只是它们并不是由模式的名称所指的.这个问题没有任何答案,但是对这些模式进行分类将为图书馆本身提供有用的见解.

推荐答案

懒惰初始化:

$(document).ready(function(){
    $('div.app').myPlugin();
});

adapter或包装器

$('div').css({
    opacity: .1 // opacity in modern browsers, filter in IE.
});

facade

// higher level interfaces (facades) for $.ajax();
$.getJSON();
$.get();
$.getScript();
$.post();

observer

// jQuery utilizes it's own event system implementation on top of DOM events.
$('div').click(function(){})
$('div').trigger('click', function(){})

iterator

$.each(function(){});
$('div').each(function(){});

策略

$('div').toggle(function(){}, function(){});

proxy

$.proxy(function(){}, obj); // =oP

builder

$('<div class="hello">world</div>');

prototype

// this feels like cheating...
$.fn.plugin = function(){}
$('div').plugin();

flyweaight

// CONFIG is shared
$.fn.plugin = function(CONFIG){
     CONFIG = $.extend({
         content: 'Hello world!'
     }, CONFIG);
     this.html(CONFIG.content);
}

其他推荐答案

复合模式在jQuery中也非常常用.在与其他图书馆合作之后,我可以看到这种模式并不像一见钟情.模式基本上说,

一组对象的处理方式与对象的单个实例相同.

例如,在处理单个DOM元素或一组DOM元素时,两者都可以均匀处理.

$('#someDiv').addClass('green'); // a single DOM element

$('div').addClass('green');      // a collection of DOM elements

其他推荐答案

singleton/module模式如何,如本文中讨论的关于yui的讨论: http://yuiblog.com/blog/2007/06/12/module-pattern/

我相信jQuery在其核心中使用了这种模式,并鼓励插件开发人员使用该模式.使用这种模式是一种使全局名称空间远离混乱的方便,有效的方法,这是通过协助开发人员编写干净,封装的代码而进一步有用的.

本文地址:https://www.itbaoku.cn/post/627649.html

问题描述

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. One obvious example would be the Decorator pattern. The jQuery object provides new and additional functionality around a regular DOM object.

For example, the DOM has a native insertBefore method but there is no corresponding insertAfter method. There are various implementations available to fill this gap, and jQuery is one such library that provides this functionality:

$(selector).after(..)
$(selector).insertAfter(..)

There are many other examples of the Decorator pattern being heavily used in jQuery.

What other examples, big or small, of design patterns have you noticed that are part of the library itself? Also, please provide an example of the usage of the pattern.

Making this a community wiki as I believe that various things people love about jQuery can be traced back to well known design patterns, just that they are not commonly referred to by the pattern's name. There is no one answer to this question, but cataloging these patterns will provide a useful insight into the library itself.

推荐答案

Lazy Initialization:

$(document).ready(function(){
    $('div.app').myPlugin();
});

Adapter or wrapper

$('div').css({
    opacity: .1 // opacity in modern browsers, filter in IE.
});

Facade

// higher level interfaces (facades) for $.ajax();
$.getJSON();
$.get();
$.getScript();
$.post();

Observer

// jQuery utilizes it's own event system implementation on top of DOM events.
$('div').click(function(){})
$('div').trigger('click', function(){})

Iterator

$.each(function(){});
$('div').each(function(){});

Strategy

$('div').toggle(function(){}, function(){});

Proxy

$.proxy(function(){}, obj); // =oP

Builder

$('<div class="hello">world</div>');

Prototype

// this feels like cheating...
$.fn.plugin = function(){}
$('div').plugin();

Flyweight

// CONFIG is shared
$.fn.plugin = function(CONFIG){
     CONFIG = $.extend({
         content: 'Hello world!'
     }, CONFIG);
     this.html(CONFIG.content);
}

其他推荐答案

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

其他推荐答案

How about the Singleton/Module pattern, as discussed in this article about YUI: http://yuiblog.com/blog/2007/06/12/module-pattern/

I believe jQuery uses this pattern in its core, as well as encouraging plug-in developers to use the pattern. Using this pattern is a handy and efficient way of keeping the global namespace clear of clutter, which is further useful by assisting developers in writing clean, encapsulated code.