Search code examples
javascriptjqueryextend

How do I prevent a jQuery extend deep copy from copying some of my custom properties?


I have some custom properties on some of my objects, which I would like to keep unique per object.

Some third party code, however, is attempting to use jQuery to .extend some of my objects - with the deep-copy flag turned on.

I would like to prevent these properties from being deep copied. My intuition is to overwrite the jQuery.extend (and jQuery.fn.extend) functions, with functions which ignore my properties, but that sounds like overkill.


Solution

  • Properties added through

    Object.defineProperty
    

    with the enumerable argument set to false won't be caught by jQuery.extend.


    a = { b: 'b'};
    Object.defineProperty(a,'bb',{ get: function() {return "bb";},enumerable: false});
    c = {};
    jQuery.extend(c,a,a,a,a);
    a.b  // "b"
    a.bb // "bb"
    c.b  // "b"
    c.bb // undefined