jQuery $.extend()用法总结
1.合并多个对象
var Css1={size: "10px",style: "oblique"}
var Css2={size: "12px",style: "oblique",weight: "bolder"}
$.jQuery.extend(Css1,Css2)
//结果:Css1的size属性被覆盖,而且继承了Css2的weight属性
// Css1 = {size: "12px",style: "oblique",weight: "bolder"}
2.深度嵌套对象
jQuery.extend(
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
//结果:{ name: “John”, last: “Resig”, location: { state: “MA” } }
--深层合并
jQuery.extend( true,
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
//结果:
{ name: “John”, last: “Resig”,
location: { city: “Boston”, state: “MA” } }
3.可以给jQuery添加静态方法。
$.extend({
add:function(a,b){return a+b;},
minus:function(a,b){return a-b},
multiply:function(a,b){return a*b;},
divide:function(a,b){return Math.floor(a/b);}
});
var sum = $.add(3,5)+$.minus(3,5)+$.multiply(3,5)+$.divide(5,7);
console.log(sum);
4.
jQuery为开发插件提拱了两个方法,分别是:
jQuery.fn.extend(object); //扩展实例
jQuery.extend(object); //扩展静态方法
$.fn.extend({name:function(){console.log("1")},age:12})
a=$('body')
a.name() //1
ps: 扩展插件还有种简单的方式,如下,因为jQuery 中 jQuery.fn.init.prototype = jQuery.fn;
$.fn.display=function{}


浙公网安备 33010602011771号