Why does jQuery Extend Deep Copy not recursively copy an object?

余生颓废 提交于 2019-12-01 15:53:46

For one, you aren't creating normal objects.

I'm looking at the source code for jQuery 1.7.2 for extend.

https://github.com/jquery/jquery/blob/master/src/core.js

And I'm noticing the line:

if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy))

has to evaluate to true to do deep copying. copy is just part of the current object being copied.

But you aren't creating "plain" objects. You are creating objects generated by invoking a constructor with the new operator.

Now, in isPlainObject, it seems these lines have to be evaluated. (where hasOwn is hasOwn = Object.prototype.hasOwnProperty

    try {
        // Not own constructor property must be Object
        if ( obj.constructor &&
            !hasOwn.call(obj, "constructor") &&
            !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
            return false;
        }
    } catch ( e ) {
        // IE8,9 Will throw exceptions on certain host objects #9897
        return false;
    }

And there's where it concludes it's not a "plainObject".

This makes sense when you consider objects with a constructor probably ought to be created via that constructor or at least use some sort of "clone" method as you'd see in other languages/frameworks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!