$.extend()

jQuery.extend扩展函数的用法(没有的新加,已有的覆盖

  • 扩展jquery静态方法
$.extend({
    test:function(){alert('test函数')}
})

用法: $.test()

  • 合并多个对象
//用法: jQuery.extend(obj1,obj2,obj3,..)
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"}
  • 深度嵌套对象

新的extend()允许你更深度的合并镶套对象。下面的例子是一个很好的证明。

// 以前的 .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” } }

 

posted @ 2016-11-23 17:09  Chris_在IT道路上前行  阅读(98)  评论(0编辑  收藏  举报