Bug Tracker

Opened 9 years ago

Closed 9 years ago

#14275 closed bug (notabug)

jQuery.extend does not copy fields value fo undefined from objectN to target

Reported by: [email protected] Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 2.0.3
Keywords: Cc:
Blocked by: Blocking:


jQuery.extend demonstrates unexpected behavior. Fields from objectN with value of undefined does not copy to target object.


var target = {};
var object1 = {
    undefined_field: undefined, 
    null_field: null
var result = $.extend({}, target, object1);

for (var field in target)
    console.log(field, target[field]);

for (var field in object1)
    console.log(field, object1[field]);

for (var field in result)
    console.log(field, result[field]);


> target:
> object1:
> undefined_field undefined
> null_field null
> result:
> null_field null

Expected output:

> target:
> object1:
> undefined_field undefined
> null_field null
> result:
> undefined_field undefined
> null_field null

Bug reproduces with jQuery versions of 1.8.3, 1.10.2, 2.0.3.

Change History (2)

comment:1 Changed 9 years ago by [email protected]

See examples by links below:
jQuery 1.8.3
jQuery 1.10.2
jQuery 2.0.3

* it's recommended to use jsFiddle.net, but it's down now

comment:2 Changed 9 years ago by dmethvin

Resolution: notabug
Status: newclosed

As documented in http://api.jquery.com/jQuery.extend/ :

Undefined properties are not copied.

We definitely wouldn't want to change documented behavior here, it certainly should be expected by those who read the documentation.

Note: See TracTickets for help on using tickets.