原文链接:http://remysharp.com/2010/06/03/signs-of-a-poorly-written-jquery-plugin/

原文作者:remy sharp

So far with every single workshop I’ve given, both for advanced JavaScript and jQuery for Designers, this question (or some variation thereof) has come up:

How do you know if the plugin is good to use?

It’s always dependant on the problem they’re trying to solve, but in lieu of a better jQuery plugin ranking system, here’s a couple of tips that should raise a red flag.

Consider the following:

$.fn.myplugin =function(){
  var me = $(this).each(function(){
    return $(this).bind('someEvent',function(){
      // does something
    });
  });   return me;
};

Although the code may be perfect once some event has run, most times you don’t have time to read through all the code carefully and you need to make a decision so you can move on to the actual problem you’re trying to solve.

In the code above, there’s a number of red flags that have gone up for me, and I tend to look in this area of code first. If these patterns have been used, it tells me the author hasn’t quite grasped how jQuery works and hasn’t considered making simple tuning changes.

The inline return

$.fn.myplugin =function(){
  var me = $(this).each(fn);
  return me;
};

Should be written as:

$.fn.myplugin =function(){
  return $(this).each(fn);
};

The me variable isn’t being used again, so there’s no point in creating it.

Double jQuery

$.fn.myplugin =function(){
  return $(this).each(fn);
};

Whilst within the context of the plugin code – i.e. within the function attached to .fn, the keyword this refers to the jQuery instance, not DOM elements.

If I were to rewrite this to show you the value, you’d see:

$.fn.myplugin =function(){
  return $($('div.foo')).each(fn);
};

So within the actual plugin (not jQuery callbacks), this refers to jQuery, so we can access jQuery’s methods directly:

$.fn.myplugin =function(){
  returnthis.each(fn);
};

Returning what to each?

$.fn.myplugin =function(){
  returnthis.each(function(){
    return $(this).bind('someEvent', fn);
  });
};

jQuery’s each iterator simply loops, it doesn’t collect anything. The result variable is jQuery with the original collection inside it still – you can’t modify the collection by returning or not returning.

So return isn’t required at all in this case:

$.fn.myplugin =function(){
  returnthis.each(function(){
    $(this).bind('someEvent', fn);
  });
};

Wasteful use of each

$.fn.myplugin =function(){
  returnthis.each(function(){
    $(this).bind('someEvent', fn);
  });
};

Hopefully by removing all the cruft from the starting version, this next step should be obvious. If not, here’s a clue:

  • What’s returned from an each call? A jQuery collection.
  • What’s returned from a bind call? A jQuery collection.

Since we’re running the bind on each element, and only doing that, it means there’s no difference. So let’s throw away the each call and just return the bind:

$.fn.myplugin =function(){
  returnthis.bind('someEvent', fn);
};

Remember that within the plugin, this refers to the jQuery instance, and not the element, so we don’t need the wrapping$().

All better now, eh?

Signs of a poorly written jQuery plugin 翻译 (Jquery插件开发注意事项,Jquey官方推荐)的更多相关文章

  1. JQuery Plugin 1 - Simple Plugin

    1. How do you write a plugin in jQuery? You can extend the existing jQuery object by writing either ...

  2. The ultimate jQuery Plugin List(终极jQuery插件列表)

    下面的文章可能出自一位奥地利的作者,  列出很多jQuery的插件.类似的网站:http://jquerylist.com/原文地址: http://www.kollermedia.at/archiv ...

  3. ollicle.com: Biggerlink – jQuery plugin

    ollicle.com: Biggerlink – jQuery plugin Biggerlink – jQuery plugin Purpose Demo Updated for jQuery 1 ...

  4. JQuery plugin ---- simplePagination.js API

    CSS Themes "light-theme" "dark-theme" "compact-theme" How To Use Step ...

  5. jQuery plugin: Autocomplete 参数及实例

    官网:jQuery plugin: Autocomplete          (注:此插件已经不再更新.它的继任者是jQuery UI的一部分,) 此插件依赖于 jquery 1.2.6 --- j ...

  6. [jQuery] 自做 jQuery Plugin - Part 1

    有時候寫 jQuery 時,常會發現一些簡單的效果可以重複利用.只是每次用 Copy & Paste 大法似乎不是件好事,有沒有什麼方法可以讓我們把這些效果用到其他地方呢? 沒錯,就是用 jQ ...

  7. Element DOM Tree jQuery plugin – Firebug like functionality | RockingCode

    Element DOM Tree jQuery plugin – Firebug like functionality | RockingCode Element DOM Tree jQuery pl ...

  8. HTML5 Video player jQuery plugin

    <!DOCTYPE html> <html lang="en" > <head> <meta charset="utf-8&qu ...

  9. JQuery Plugin 开发

    学习 JQuery 插件开发之后, 可以将自己平时常用的功能封装成插件, 便于在不同的项目之间使用. JQuery 官网上的 插件开发教程就很不错, 简单易懂. 参考网址: http://learn. ...

随机推荐

  1. NSDictionary 键值对查找

           NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys: @"1",@"a", ...

  2. Android getActionBar()报空指针异常

    1. 加载完视图后,再去获取: 写在setContentView()后面. 2.sdk版本: Actionbar的主题在3.0以后才有,使用的时候要确保,最低的版本不能小于3.0. <uses- ...

  3. android从应用到驱动之—camera(1)---程序调用流程

    一.开篇 写博客还得写开篇介绍,可惜,这个不是我所擅长的.就按我自己的想法写吧. 话说camera模块,从上层到底层一共包含着这么几个部分: 1.apk------java语言 2.camera的ja ...

  4. 机器学习 —— 概率图模型(Homework: StructuredCPD)

    Week2的作业主要是关于概率图模型的构造,主要任务可以分为两个部分:1.构造CPD;2.构造Graph.对于有向图而言,在获得单个节点的CPD之后就可依据图对Combine CPD进行构造.在获得C ...

  5. 转:在MyEclipse+Hibernate

    源地址http://blog.renren.com/share/270733118/5528463779 1.实验环境准备 MyEclipse8.6 Tomcat6.0.20 MySQL5.1 ...

  6. 图像二值化----otsu(最大类间方差法、大津算法)

    最大类间方差法是由日本学者大津于1979年提出的,是一种自适应的阈值确定的方法,又叫大津 法,简称OTSU.它是按图像的灰度特性,将图像分成背景和目标2部分.背景和目标之间的类间方差越大,说明构成图像 ...

  7. smartcomb:用php实现的web模块拼合器

    smartcomb是一个用php实现的web模块拼合器,相对于其他的代码拼合工具,如下特性: 可以拼合任意类型的文件,不限于js文件. 集中并声明依赖,自动分析依赖拼合,按需加载. 支持多种配置切换 ...

  8. Android Studio安装后第一次进不去

    Android Studio 安装后第一次进不去,因为检查到有更新的SDK,在下载.但是呢,没有FQ的情况下,无法下载下来,所以就卡住了. 那么解决方案就是让 Android Studio 第一次启动 ...

  9. git for windows+TortoiseGit客户端的使用

        一.安装Git客户端 全部安装均采用默认! 1. 安装支撑软件 : https://code.google.com/p/msysgit/downloads/list?q=full+instal ...

  10. jquery ajax请求 清除缓存

    使用jquery里load方法或者ajax调用页面的时候会存在cache的问题,清除cache的方法: 调用jQuery.ajaxSetup ({cache:false}) 方法即可.