转载  jQuery.extend()的合并对象功能--默认参数的由来

分类: 2016-12-11T16:46:43    219人阅读   

jQuery.extend( [ deep ], target, object1, [ objectN ] )合并对象到第一个对象    

//deep为boolean类型,其它参数为object类型

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

var object2 = { banana: {price: 200}, durian: 100};

 

实例1:

$.extend(object1, object2); //合并对象,修改第一个对象

object1 === {apple: 0, banana: {price: 200}, cherry: 97, durian: 100} //结果

 

实例2:

var empty = {}

var object = $.extend(empty , object1, object2);  //有target, object1,object2参数

//合并对象到empty中,没有破坏已有的对象(object1)。object===empty

empty === {apple: 0, banana: {price: 200}, cherry: 97, durian: 100}

 

实例3:

$.extend(true, object1, object2); //合并对象,修改第一个对像 deep==true;

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

分享到: