Play Google

玩转谷歌--学习笔记,成长历程。

« Mootools 文档下载

Jquery插件开发(1)

原文网址:

http://www.learningjquery.com/2007/10/a-plugin-development-pattern

I've been developing jQuery plugins for quite a while now, and I've become rather comfortable with a particular style of plugin development for my scripts. This article is meant to share the pattern that I've found especially useful for plugin authoring. It assumes you already have an understanding of plugin development for jQuery; if you're a novice plugin author, please review the jQuery Authoring Guidelines first.

There are a few requirements that I feel this pattern handles nicely:

Claim only a single name in the jQuery namespace
Accept an options argument to control plugin behavior
Provide public access to default plugin settings
Provide public access to secondary functions (as applicable)
Keep private functions private
Support the Metadata Plugin
I'll cover these requirements one by one, and as we work through them we'll build a simple plugin which highlights text.


Claim only a single name in the jQuery namespace
This implies a single-plugin script. If your script contains multiple plugins, or complementary plugins (like $.fn.doSomething() and $.fn.undoSomething()) then you'll claim multiple names are required. But in general when authoring a plugin, strive to use only a single name to hold all of its implementation details.

我试着翻译了一下(偶英语很菜),大致意思是:

我开发JQuery插件有一段时间了,并且我能够很自由的按照我自己的模式开发脚本。本文的目的是共享我这种特别有用的插件开发模式。这篇文章的内容假定您已经了解了开发一个JQUERY插件的基础知识,如果您还不了解,请点看jQuery Authoring Guidelines 。

以下是我认为开发一个好的插件的几个要点:

1.用单一的名字( a single name )来声明JQUERY 命名空间。

2.通过一组参数来控制插件的行为。

3.提供默认设置的公共入口。

4.提供次要函数(secondary functions )的公共入口。

5.保证私有函数是私有的。

6.支持数据插件。

我将通过创建一个简单的高亮文本的插件来逐一介绍这些要点。

Claim only a single name in the jQuery namespace
This implies a single-plugin script. If your script contains multiple plugins, or complementary plugins (like $.fn.doSomething() and $.fn.undoSomething()) then you'll claim multiple names are required. But in general when authoring a plugin, strive to use only a single name to hold all of its implementation details.

And our plugin can be invoked like this:

$('#myDiv').hilight();

But what if we need to break up our implementation into more than one function? There are many reasons to do so: the design may require it; it may result in a simpler or more readable implementation; and it may yield better OO semantics.

It's really quite trivial to break up the implementation into multiple functions without adding noise to the namespace. We do this by recognizing, and taking advantage of, the fact that functions are first-class objects in JavaScript. Like any other object, functions can be assigned properties. Since we have already claimed the "hilight" name in the jQuery prototype object, any other properties or functions that we need to expose can be declared as properties on our "hilight" function. More on this later.

1.用单一的名字( a single name )来声明JQUERY 命名空间。

这意味着一个单一插件的脚本。如果你的脚本包含多个插件,或者是一对互补的插件如(如 $.fn.doSomething() and $.fn.undoSomething())那么声明多个名称是必须了。但是通常创建一个插件时,尽量用一个单一的名字来提供所有的实现细节。
In our example plugin we will claim the name "hilight".
在我们的示例插件中,我们将声明一个名字“hilight”

// plugin definition
$.fn.hilight = function() {
  // Our plugin implementation code goes here.
}; 

 我们能这样使用这个插件:

$('#myDiv').hilight();

但是有什么理由让我们把我们的实现拆分成多个方法?有许多理由这样做:设计需要这样做,这样可以使我们的实现更加简洁可读,并且这样更符合面向对象(00)的语法。

 

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

vinehoo,web2.0葡萄酒社区
vinehoo,web2.0葡萄酒社区

日历

最新评论及回复

最近发表

Powered By Karry 

Copyright © 2008 PlayGoogle.com. All Rights Reserved.