Skip to content

jQuery.extend with deep=true causes unpredicted side effects #2773

@maciej-pawlisz

Description

@maciej-pawlisz

There is a problem with deep extending - nested objects get overritten, which might be very confusing (example at: https://jsfiddle.net/4gpju44c/):

var nested={x:1}
var obj={n:nested}
$.extend(true,obj,{n:{x:2,y:2}}) 
console.log(nested)
{x:2, y:2, } 

It is even worse, when nested will be a prototype - we can get very unexpected errors (I experienced it when using jquery.rest and I changed verbs)

In jQuery.extend function there is a comment, which suggests that this situation should be avoided :

         // Never move original objects, clone them
        target[ name ] = jQuery.extend( deep, clone, copy );

but it is not working as the comment suggests. Maybe it should be replaced with:

        target[ name ] = jQuery.extend( deep,{}, clone, copy );

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions