简体   繁体   English

有关jQuery模式的问题

[英]Questions about jQuery patterns

So i decided to finally learn something about jQuery and as i needed a simple function for one of my projects, i started to search for a good pattern. 因此,我决定最终学习一些有关jQuery的知识,由于我需要为我的一个项目提供一个简单的功能,因此我开始寻找一种好的模式。

I began with the official guide but soon found many other possible templates. 我从官方指南开始,但很快发现了许多其他可能的模板。 I will reference two of them to ask my questions: 我将参考其中两个提出我的问题:

First one 第一

Second one 第二个

The first pattern seems to be a lot more cleaner to me, i like the idea of the namespace very much. 第一种模式对我来说似乎更加干净,我非常喜欢名称空间的想法。 But, how is this used? 但是,如何使用? Do i write my whole functions as methods of the namespace, then calling all of them in the init() and finally just call this one method init() in the IIFE , or should i call the necessary methods directly in the IIFE ? 我是否将整个函数编写为命名空间的方法,然后在init()调用所有函数,最后只在IIFE中调用此方法init() ,还是应该在IIFE中直接调用必要的方法?

I feel like this question is very idiotic, but i just can't understand the usage. 我觉得这个问题很白痴,但我只是不明白用法。

The second pattern i even more complicated to me. 第二种方式对我来说更加复杂。 Have a look at this: 看看这个:

;(function ( $, window, document, undefined ) {
    //...
})( jQuery, window, document );

What are all these parameters, where do i set them and why are they needed? 这些参数是什么,我在哪里设置它们,为什么需要它们? What is the disadvantage of the wrapper in the first sample? 第一个样本中包装纸的缺点是什么?

The diversity of possibilities is overwhelming, i don't know where to start or how to figure ot the right thing for me. 可能性的多样性是压倒性的,我不知道从哪里开始或如何找到适合我的东西。

Try Javascript Revealing Module Pattern. 尝试使用JavaScript揭示模块模式。

From my experience this one is the best one. 根据我的经验,这是最好的。

Follow it on Javascript Revealing Module Pattern Javascript揭示模块模式上关注它

Check this excellent article on Smashing Magazine . Smashing Magazine上查看这篇出色的文章。
it covers multitudes of jQuery plugin patterns and explains each and every one of them. 它涵盖了许多jQuery插件模式,并解释了每种模式。

Edit: 编辑:
There is a portion of the article there that answers your question :) 那里有一部分文章回答了您的问题:)

From the Docs of Plugins/Authoring - jQuery Wiki 来自插件/创作文档-jQuery Wiki

To write a jQuery plugin, start by adding a new function property to the jQuery.fn object where the name of the property is the name of your plugin: 要编写jQuery插件,请在jQuery.fn对象中添加一个新的function属性,其中该属性的名称为您的插件的名称:

jQuery.fn.myPlugin = function() {
  // Do your awesome plugin stuff here
};

The above one is the simplest of the plugin making. 上面的是最简单的插件制作。 Although there is a specific steps to follow, using a closure is the best practice to avoid any $ dollar sign conflict with any other libraries like mootools, prototype etc. 虽然具体执行的步骤,使用闭包是避免任何的最佳做法$有喜欢Moot,样机等任何其他图书馆美元符号冲突

To make sure that your plugin doesn't collide with other libraries that might use the dollar sign, it's a best practice to pass jQuery to an IIFE (Immediately Invoked Function Expression) that maps it to the dollar sign so it can't be overwritten by another library in the scope of its execution. 为了确保您的插件不会与其他可能使用美元符号的库发生冲突,最好的做法是将jQuery传递给IIFE(立即调用函数表达式),该IIFE将其映射到美元符号,因此不会被覆盖由另一个库在其执行范围内执行。

 (function( $ ) {
   $.fn.myPlugin = function() {
    // Do your awesome plugin stuff here
   };
})(jQuery);

Now within that closure, we can use the dollar sign in place of jQuery as much as we like. 现在在该闭包中,我们可以根据需要使用美元符号代替jQuery。

Namespacing : 命名空间:

Properly namespacing your plugin is a very important part of plugin development. 正确命名插件空间是插件开发的重要组成部分。 Namespacing correctly assures that your plugin will have a very low chance of being overwritten by other plugins or code living on the same page. 正确命名空间可确保您的插件被同一个页面上的其他插件或代码覆盖的可能性很小。 Namespacing also makes your life easier as a plugin developer because it helps you keep better track of your methods, events and data. Namespacing还使您作为插件开发人员的生活更加轻松,因为它可以帮助您更好地跟踪方法,事件和数据。

Plugin Methods 插件方法

(function( $ ){
  $.fn.tooltip = function( options ) { 
    // THIS
  };
  $.fn.tooltipShow = function( ) {
    // IS
  };
  $.fn.tooltipHide = function( ) { 
    // BAD
  };
  $.fn.tooltipUpdate = function( content ) { 
    // !!!  
  };
})(jQuery);

This is a discouraged because it clutters up the $.fn namespace. 不建议这样做,因为它会使$.fn名称空间$.fn To remedy this, you should collect all of your plugin's methods in an object literal and call them by passing the string name of the method to the plugin. 为了解决这个问题,您应该将所有插件方法收集到一个object literal ,并通过将方法的字符串名称传递给插件来调用它们。

 (function( $ ){
     var methods = {
        init : function( options ) { 
              // THIS 
        },
        show : function( ) {
              // IS
        },
        hide : function( ) { 
              // GOOD
        },
        update : function( content ) { 
              // !!! 
        }
     };

  $.fn.tooltip = function( method ) {
   // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    
   };
 })( jQuery );

// calls the init method
 $('div').tooltip(); 

 // calls the init method
 $('div').tooltip({
    foo : 'bar'
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM