var object1 = {apple: 0,banana: {weight: 52, price: 100},cherry: 97};
var object2 = {banana: {price: 200},durian: 100};
$.extend(object1, object2);
//{apple: 0, banana: {price:200}, cherry: 97, durian: 100}
$.extend(true, object1, object2);
//{apple: 0, banana: {weight: 52, price:200}, cherry: 97, durian: 100}
jQuery.fn.extend()
;(function($, window, document, undefined){
//Plugin code here
})(jQuery, window, document);
//方式1
;(function($, window, document, undefined){
$.pluginName = function(){
//Plugin implementation code here
};
})(jQuery, window, document);
//方式2 当全局函数较多时
;(function($, window, document, undefined){
$.extend({
pluginName = function(){
//Plugin code here
};
})
})(jQuery, window, document);
//方式1
;(function($, window, document, undefined){
$.fn.pluginName = function(options) {
return this.each(function() {
//this关键字代表了这个插件将要执行的jQuery对象
//return this.each()使得插件能够形成链式调用
var defaults = {
//pro : value
};
var settings = $.extend({}, defaults, options);
// plugin implementationcode here
});
}
})(jQuery, window, document);
//方式2
;(function($, window, document, undefined){
$.fn.extend({
pluginName : function(){
return this.each(function(){
// plugin code here
});
};
})
})(jQuery, window, document);
//方式3 这种类型的插件架构允许您封装所有的方法在父包中,通过传递该方法的字符串名称和额外的此方法需要的参数来调用它们。
;(function($, window, document, undefined){
// 在我们插件容器内,创造一个公共变量来构建一个私有方法
var privateFunction = function() {
// code here
}
// 通过字面量创造一个对象,存储我们需要的公有方法
var methods = {
// 在字面量对象中定义每个单独的方法
init: function() {
// 为了更好的灵活性,对来自主函数,并进入每个方法中的选择器其中的每个单独的元素都执行代码
return this.each(function() {
// 为每个独立的元素创建一个jQuery对象
var $this = $(this);
// 创建一个默认设置对象
var defaults = {
propertyName: 'value',
onSomeEvent: function() {}
}
// 使用extend方法从options和defaults对象中构造出一个settings对象
var settings = $.extend({}, defaults, options);
// 执行代码
// 例如: privateFunction();
});
},
destroy: function() {
// 对选择器每个元素都执行方法
return this.each(function() {
// 执行代码
});
}
};
$.fn.pluginName = function() {
// 获取我们的方法,遗憾的是,如果我们用function(method){}来实现,这样会毁掉一切的
var method = arguments[0];
// 检验方法是否存在
if(methods[method]) {
// 如果方法存在,存储起来以便使用
// 注意:我这样做是为了等下更方便地使用each()
method = methods[method];
// 如果方法不存在,检验对象是否为一个对象(JSON对象)或者method方法没有被传入
} else if( typeof(method) == 'object' || !method ) {
// 如果我们传入的是一个对象参数,或者根本没有参数,init方法会被调用
method = methods.init;
} else {
// 如果方法不存在或者参数没传入,则报出错误。需要调用的方法没有被正确调用
$.error( 'Method ' + method + ' does not exist on jQuery.pluginName' );
return this;
}
// 调用我们选中的方法
// 再一次注意我们是如何将each()从这里转移到每个单独的方法上的
return method.call(this);
}
})(jQuery, window, document);
//方式4 面向对象的插件开发 将原型和构造函数组合使用,使得通过构造函数创建的每个实例都能继承相关属性与方法
;(function($, window, document, undefined){
//定义Beautifier的构造函数
var Beautifier = function(ele, opt) {
this.$element = ele;
this.defaults = {
'color': 'red',
'fontSize': '12px',
'textDecoration':'none'
};
this.options = $.extend({}, this.defaults, opt);
}
//定义Beautifier的原型方法
Beautifier.prototype = {
beautify: function() {
return this.$element.css({
'color': this.options.color,
'fontSize': this.options.fontSize,
'textDecoration': this.options.textDecoration
});
}
}
//在插件中使用Beautifier对象
$.fn.myPlugin = function(options) {
//创建Beautifier的实体
var beautifier = new Beautifier(this, options);
//调用其方法
return beautifier.beautify();
}
})(jQuery, window, document);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有