Latest web development tutorials

jQuery.extend () method

jQuery Misc Methods jQuery Misc Methods

Examples

Through the array elements, and modify the first object

<Div id = "log"> </ div> <script> $ (function () { var object1 = { apple: 0, banana: {weight : 52, price: 100}, cherry: 97 }; Var object2 = { banana: {price: 200}, durian: 100 }; / * Object2 into object1 in * / $ 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) );}) </ Script>

try it"

Definition and Usage

jQuery.extend () function is used to the contents of one or more objects into the target object.

Note: 1. If only for $ .extend () specifies a parameter, it means that the target parameter is omitted. In this case, target is the jQuery object itself. In this way, we can add a new global object jQuery function.
2. If multiple objects have the same property, which overrides the property value of the former.


grammar

$.extend( target [, object1 ] [, objectN ] )

Indicates whether the combined depth


$.extend( [deep ], target, object1 [, objectN ] )

Warning: do not support the first argument false.

参数 描述
deep 可选。 Boolean类型 指示是否深度合并对象,默认为false。如果该值为true,且多个对象的某个同名属性也都是对象,则该"属性对象"的属性也将进行合并。
target Object类型 目标对象,其他对象的成员属性将被附加到该对象上。
object1 可选。 Object类型 第一个被合并的对象。
objectN 可选。 Object类型 第N个被合并的对象。


Examples

More examples

Recursively merge the two objects
Recursively merge the two objects, and modifying the first object.

Merge defaults and options objects
Merge defaults and options object, and the object does not modify the defaults. This is a common plug-in development mode.


jQuery Misc Methods jQuery Misc Methods