標籤:prot color tip 靜態 john style play size 方法
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{}
jQuery $.extend()用法總結