Bug Tracker

Opened 6 years ago

Closed 6 years ago

#14275 closed bug (notabug)

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

Reported by: sempasha@… 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.

Change History (2)

comment:1 Changed 6 years ago by sempasha@…

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