Skip to main content

Bug Tracker

Side navigation

#14275 closed bug (notabug)

Opened August 20, 2013 02:22PM UTC

Closed August 20, 2013 05:43PM UTC

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

Reported by: sempasha@gmail.com Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 2.0.3
Keywords: Cc:
Blocked by: Blocking:
Description

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

Example:

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

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

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

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

Output:

> 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.

Attachments (0)
Change History (2)

Changed August 20, 2013 02:40PM UTC by sempasha@gmail.com comment:1

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''

Changed August 20, 2013 05:43PM UTC by dmethvin comment:2

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.