1. 了解extend
jQuery.extend(
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
=》{ name: “John”, last: “Resig”, location: { state: “MA” } }
更深入的extend:
jQuery.extend(
true,
{ name: “John”, location: { city: “Boston” } },
{ last: “Resig”, location: { state: “MA” } }
);
=》 { name: “John”, last: “Resig”, location: { city: “Boston”, state: “MA” } }
2. jQuery.extend(object) / $.extend(object)扩展了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);
经过例子2可以看出extend可以扩展$,为其增加新的对象,方便我们使用(如:$.add(3,5))