Signs of a poorly written jQuery plugin 翻译 (Jquery插件开发注意事项,Jquey官方推荐)
原文链接: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
eachcall? A jQuery collection. - What’s returned from a
bindcall? 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官方推荐)的更多相关文章
- JQuery Plugin 1 - Simple Plugin
1. How do you write a plugin in jQuery? You can extend the existing jQuery object by writing either ...
- The ultimate jQuery Plugin List(终极jQuery插件列表)
下面的文章可能出自一位奥地利的作者, 列出很多jQuery的插件.类似的网站:http://jquerylist.com/原文地址: http://www.kollermedia.at/archiv ...
- ollicle.com: Biggerlink – jQuery plugin
ollicle.com: Biggerlink – jQuery plugin Biggerlink – jQuery plugin Purpose Demo Updated for jQuery 1 ...
- JQuery plugin ---- simplePagination.js API
CSS Themes "light-theme" "dark-theme" "compact-theme" How To Use Step ...
- jQuery plugin: Autocomplete 参数及实例
官网:jQuery plugin: Autocomplete (注:此插件已经不再更新.它的继任者是jQuery UI的一部分,) 此插件依赖于 jquery 1.2.6 --- j ...
- [jQuery] 自做 jQuery Plugin - Part 1
有時候寫 jQuery 時,常會發現一些簡單的效果可以重複利用.只是每次用 Copy & Paste 大法似乎不是件好事,有沒有什麼方法可以讓我們把這些效果用到其他地方呢? 沒錯,就是用 jQ ...
- Element DOM Tree jQuery plugin – Firebug like functionality | RockingCode
Element DOM Tree jQuery plugin – Firebug like functionality | RockingCode Element DOM Tree jQuery pl ...
- HTML5 Video player jQuery plugin
<!DOCTYPE html> <html lang="en" > <head> <meta charset="utf-8&qu ...
- JQuery Plugin 开发
学习 JQuery 插件开发之后, 可以将自己平时常用的功能封装成插件, 便于在不同的项目之间使用. JQuery 官网上的 插件开发教程就很不错, 简单易懂. 参考网址: http://learn. ...
随机推荐
- Java多线程3:Thread中start()和run()的区别
原文:http://www.cnblogs.com/skywang12345/p/3479083.html start() 和 run()的区别说明start():它的作用是启动一个新线程,新线程会执 ...
- 用paint 计算字符串的像素宽度
方法1: //直接返回参数字符串所占用的像素宽度 Paint paint = new Paint(); width = paint.measureText(str); 有一些view可以直接得到pai ...
- [51NOD]BSG白山极客挑战赛
比赛链接:http://www.51nod.com/contest/problemList.html#!contestId=21 /* ━━━━━┒ギリギリ♂ eye! ┓┏┓┏┓┃キリキリ♂ min ...
- useradd和adduser的区别
1. 在root权限下,useradd只是创建了一个用户名,如 (useradd +用户名 ),它并没有在/home目录下创建同名文件夹,也没有创建密码,因此利用这个用户登录系统,是登录不了的,为了 ...
- hdu 1885 Key Task (三维bfs)
题目 之前比赛的一个题, 当时是崔老师做的,今天我自己做了一下.... 还要注意用bfs的时候 有时候并不是最先到达的就是答案,比如HDU 3442 这道题是要求最小的消耗血量伤害,但是并不是最先到 ...
- 玩转EasyUi弹出框
这两天在搞EasyUi的弹出框,弹出框之前也搞过很多个版本,总是觉得不那么完美,刚好最近有时间,就往多处想了想,功能基本上达到我的预期,并且在开发过程中遇到很多小技巧,特撰文如下. 走起:在EasyU ...
- bzoj2326: [HNOI2011]数学作业
矩阵快速幂,分1-9,10-99...看黄学长的代码理解...然而他直接把答案保存在最后一行(没有说明...好吧应该是我智障这都不知道... #include<cstdio> #inclu ...
- Android获取SharedPreferences失败,且App无法启动
说明: 一个app访问另外一个app的SharedPreferences,程序启动之后没有显示界面就卡死了,无任何提示信息. 错误原因: 应用调用createPackageContext失败,但是也不 ...
- OK335xS psplash 进度条工作原理 hacking
#!/bin/sh # # rc This file is responsible for starting/stopping # services when the runlevel changes ...
- 用Rational Rose来建立数据库表
这里以MS SQL Server2000中已有的一个Northwind库为例,我们命名新的数据库名为NorthwindRose:我们只挑其中的两个表Customers和Employees做示例,另外我 ...