jquery插件的两种形式
这里总结一下jquery插件的两种形式,一种是通过字面量的形式组织代码,另一种是通过构造函数的方式。下面就两种形式来分析俩个例子。
例子1:
;(function ($,window,document,undefined) {
$.fn.changeColor= function (option) {
var defined={
width:"400",
height:"500",
color:"red",
module:"animate"
};
var setting= $.extend({},defined,option);
var func={
changeColor: function (obj,setting) {
obj.css({
width:setting.width,
height:setting.height,
backgroundColor:setting.color
})
},
animateColor: function (obj, setting) {
obj.animate({
width:setting.width,
height:setting.height,
},3000)
}
};
return this.each(function () {
if(setting.module==="animate"){
func.animateColor($(this),setting);
}else if(setting.module==="css"){
func.changeColor($(this),setting);
}
})
} })(jQuery,window,document);
通过代码可以看到,所有的局部函数都放在了$.fn.changeColor
中,在里面通过func
这个对象来组织函数,最后通过return
把jquery对象返回出去实现链式调用。
例子2:
;(function ($,window,document,undefined) {
上面的例子中采用的是构造函数的方式,先在
var changeStyle= function (option) {
var defined={
width:"500",
height:"300",
color:"red"
};
this.setting= $.extend({},defined,option);
console.log(this.setting)
};
changeStyle.prototype={
styleColor: function (ele) {
return ele.css({
backgroundColor:this.setting.color
})
},
styleWidth: function (ele) {
return ele.css({
width:this.setting.width
});
},
styleHeight: function (ele) {
return ele.css({
////这里的return为的是在返回change.styleWidth()这个函数的基础上,把ele暴露出去。
height:this.setting.height
})
}
};
$.fn.changeStyle= function (option) {
var change=new changeStyle(option);
if(option.style==="width"){
return change.styleWidth($(this));
//这里的return为的是返回change.styleWidth()这个函数,暴露出去。
}else if(option.style==="height"){
return change.styleHeight($(this));
}else if(option.style==="color"){
return change.styleColor($(this))
}
} })(jQuery,window,document);changeStyle
函数中传入参数并挂接到原型上,等待后续调用。然后把具体的函数,挂接到原型对象上,changeStyle.prototype
。这里需要注意的两点是这里用到了两次return
,为的是能把jqery对象暴露到外层,供调用。
jquery插件的两种形式的更多相关文章
- jQuery中开发插件的两种方式
jQuery中开发插件的两种方式(附Demo) 做web开发的基本上都会用到jQuery,jQuery插件开发两种方式:一种是类扩展的方式开发插件,jQuery添加新的全局函数(jQuery的全局函数 ...
- Js类的静态方法与实例方法区分以及jQuery如何拓展两种方法
上学时C#老师讲到对象有两类方法,静态方法(Static)和实例方法(非Static),当时不理解静态是为何意,只是强记. 后来从事前端工作,一直在对类(即对象,Js中严格来说没有类的定义,虽众所周知 ...
- 基于 Scrapy-redis 两种形式的分布式爬虫
基于 Scrapy-redis 两种形式的分布式爬虫 .caret, .dropup > .btn > .caret { border-top-color: #000 !important ...
- C++:一般情况下,设计函数的形参只需要两种形式
C++:一般情况下,设计函数的形参只需要两种形式.一,是引用形参,例如 void function (int &p_para):二,是常量引用形参,例如 void function(const ...
- SQL 关于apply的两种形式cross apply 和 outer apply(转)
转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...
- jQuery插件开发的两种方法及$.fn.extend的详解
jQuery插件开发分为两种: 1 类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax(...),相当于静态方法. 开发扩展其方法时使用$.extend方法,即jQuery.ex ...
- SQL 关于apply的两种形式cross apply 和 outer apply
SQL 关于apply的两种形式cross apply 和 outer apply 例子: CREATE TABLE [dbo].[Customers]( ) COLLATE Chinese_PRC_ ...
- SQL关于apply的两种形式cross apply和outer apply(转载)
SQL 关于apply的两种形式cross apply 和 outer apply apply有两种形式: cross apply 和 outer apply 先看看语法: <lef ...
- 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编
在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编 ...
随机推荐
- 主机信息收集工具DMitry
主机信息收集工具DMitry DMitry是Kali Linux内置的一款信息收集工具.它的目标主要是Web类主机.它不仅通过主动查询.端口扫描方式,还借助第三方网站和搜索引擎获取信息. 它搜集的 ...
- SNMP高速扫描器braa
SNMP高速扫描器braa SNMP(Simple Network Monitoring Protocol,简单网络管理协议)是网络设备管理标准协议.为了便于设备管理,现在联入网络的智能设备都支持 ...
- 巴特沃斯(Butterworth)滤波器 (2) - 双线性变换
这里接着上篇讲一下双线性变换Bilinear Transformation,它实现了模拟信号(连续域)与数字信号(离散域)之间的转换. 双线性变换公式如下: 反推可得到: 因此可以根据连续域传递函数推 ...
- eclipse安装hibernate-Tools
启动eclipse 选择Help -> About Eclipse 记住自己的eclipse版本 访问http://download.jboss.org/jbosstools/updates/s ...
- linux shell basic command
Learning basic Linux commands Command Description $ ls This command is used to check the contents of ...
- WindowManager massge和handler
在一个可移动浮动按钮的demo源码学习中,有一些WindowManager的使用,在此做下总结. 1.翻译过来就是窗口管理,是和应用框架层的窗口管理器交互的接口,通过 mWindowManager = ...
- Unity Android加密DLL笔记
unity mono 地址:https://github.com/Unity-Technologies/mono 下载与unity版本对应的unity mono. brew安装:http://brew ...
- 学习安装并配置前端自动化工具Gulp
Gulp和所有Gulp插件都是基于nodeJs来运行的,因此在你的电脑上需要安装nodeJs,安装过程请移驾安装并配置前端自动化工具--grunt.安装完成后,通过运行cmd进入DOS命令窗口,如图: ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
- 异常处理_Maven多模块web项目整合ssm+dubbo
异常如下: [ERROR][org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader. ...