今天很高兴,学一学jquery.extend().

最近可是长见识了, 摆弄了好几天的ubuntu, 今天终于是顺顺利利的,听话了,用起来很顺手, 很顺手的。。。

今天也在主页嫁了雪花效果, 这不是重点, 重点是我也知道了怎么在博客圆主页假如自己的代码, 以前看到别人的主页那么风光,心里总是很嫉妒, 为什么你的那么好看,哈哈, 今天俺的主页终于也有效果了。

另外的我的ubuntu 缺少一个桌面版的词典,不知如何是好。

今天 在 看 雪花插件的时候, 有看见了
  $.extends(...);
这个问题困扰我好久了, 终于鼓起勇气 去了jquery官网。
Merge the contents of two or more objects together into the first object.

   还没靠六级的时候, 我都没仔细看, 现在看一下,  原来如此简单。

   就是合并多个对象吧。

jQuery.extend( target [, object1] [, objectN] )
target 
  An object that will receive the new properties if additional objects are passed in
  or that will extend the jQuery namespace if it is the sole argument. object1An
  object containing additional properties to merge in. objectN
  Additional objects containing properties to merge in.
jQuery.extend( [deep], target, object1 [, objectN] ) deep
  If true, the merge becomes recursive (aka. deep copy). target
  The object to extend. It will receive the new properties. object1
  An object containing additional properties to merge in. objectN
  Additional objects containing properties to merge in.

也就是将多个对象 合并到 target, 另外  官网还说:

If only one argument is supplied to $.extend(), this means the target argument was omitted(是遗漏,省略的意思). 
In this case, the jQuery object itself is assumed to be the target.(这样的话,jquery就是target)
By doing this, we can add new functions to the jQuery namespace.
This can be useful for plugin authors wishing to add new methods to JQuery.
Keep in mind that the target object (first argument) will be modified(给定的target会被修改),and will also be returned from $.extend()(也会被返回). 
If, however, we want to preserve both of the original objects, we can do so by passing an empty object as the target:
(如果我们不想让原来的对象改变, 传一个空对象进去)       var object
= $.extend({}, object1, object2);

 

后面的递归 和null元素不会被复制, 就不看了。。

 

 

最后 , 官方的例子:

  1.

var object1 = {
  apple: 0,
  banana: {weight: 52, price: 100},
  cherry: 97
};
var object2 = {
  banana: {price: 200},
  durian: 100
};

/* merge object2 into object1 */
$.extend(object1, object2);

var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) {
  var arr = [];
  $.each(obj, function(key, val) {
    var next = key + ": ";
    next += $.isPlainObject(val) ? printObj(val) : val;
    arr.push( next );
  });
  return "{ " +  arr.join(", ") + " }";
};

$("#log").append( printObj(object1) );
// {"apple":0,"banana":{"price":200},"cherry":97,"durian":100}

可以看到。。原来的object1 被修了。

  2.

var object1 = {
  apple: 0,
  banana: {weight: 52, price: 100},
  cherry: 97
};
var object2 = {
  banana: {price: 200},
  durian: 100
};

/* merge object2 into object1, recursively */
$.extend(true, object1, object2);
// true 可以递归 复制/
var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) { var arr = []; $.each(obj, function(key, val) { var next = key + ": "; next += $.isPlainObject(val) ? printObj(val) : val; arr.push( next ); }); return "{ " + arr.join(", ") + " }"; }; $("#log").append( printObj(object1) );
// {"apple":0,"banana":{"weight":52,"price":200},"cherry":97,"durian":100}

  3.

var defaults = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };

/* merge defaults and options, without modifying defaults */
var settings = $.extend({}, defaults, options);
// 看到这里我总算懂了,defaults是默认属性, options是用户改后的属性,或者是用户提交的属性, 然后合并,如果用户没有给出的就按默认的, 给了的就按用户的。
var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) { var arr = []; $.each(obj, function(key, val) { var next = key + ": "; next += $.isPlainObject(val) ? printObj(val) : val; arr.push( next ); }); return "{ " + arr.join(", ") + " }"; }; $("#log").append( "<div><b>defaults -- </b>" + printObj(defaults) + "</div>" ); $("#log").append( "<div><b>options -- </b>" + printObj(options) + "</div>" ); $("#log").append( "<div><b>settings -- </b>" + printObj(settings) + "</div>" );
/*    
  defaults -- 
{"validate":false,"limit":5,"name":"foo"}
  options -- {"validate":true,"name":"bar"}
  settings -- {"validate":true,"limit":5,"name":"bar"}
*/

恩, 写到这里  我已經很清楚了。

posted @ 2012-12-25 21:51  Onakaumi  阅读(327)  评论(0编辑  收藏  举报