ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

jQuery extend

2019-06-28 09:50:04  阅读:261  来源: 互联网

标签:jQuery Object target extend object will properties


https://api.jquery.com/jquery.extend/

jQuery.extend( target [, object1 ] [, objectN ] )

Description: Merge the contents of two or more objects together into the first object.

  • target Type: Object An object that will receive the new properties if additional objects are passed in or that will extend the jQuery namespace if it is the sole argument.
  • object1 Type: Object An object containing additional properties to merge in.
  • objectN Type: Object Additional objects containing properties to merge in.

jQuery.extend( [deep ], target, object1 [, objectN ] )

  • deep Type: Boolean If true, the merge becomes recursive (aka. deep copy). Passing false for this argument is not supported.
  • target Type: Object The object to extend. It will receive the new properties.
  • object1 Type: Object An object containing additional properties to merge in.
  • objectN Type: Object Additional objects containing properties to merge in.

When two or more object arguments are supplied to $.extend(), properties from all of the objects are added to the target object. Arguments that are null or undefined are ignored.

If only one argument is supplied to $.extend(), this means the target argument was omitted. In this case, the jQuery object itself is assumed to be the target. By doing this, you can add new functions to the jQuery namespace. This can be useful for plugin authors wishing to add new methods to JQuery.

Keep in mind that the target object (first argument) will be modified, and will also be returned from $.extend(). If, however, you want to preserve both of the original objects, you can do so by passing an empty object as the target:

1 var object = $.extend({}, object1, object2);

The merge performed by $.extend() is not recursive by default; if a property of the first object is itself an object or array, it will be completely overwritten by a property with the same key in the second or subsequent object. The values are not merged. This can be seen in the example below by examining the value of banana. However, by passing true for the first function argument, objects will be recursively merged.

Warning: Passing false for the first argument is not supported.

Undefined properties are not copied. However, properties inherited from the object's prototype will be copied over. Properties that are an object constructed via new MyCustomObject(args), or built-in JavaScript types such as Date or RegExp, are not re-constructed and will appear as plain Objects in the resulting object or array.

On a deep extend, Object and Array are extended, but object wrappers on primitive types such as String, Boolean, and Number are not. Deep-extending a cyclical data structure will result in an error.

For needs that fall outside of this behavior, write a custom extend method instead, or use a library like lodash

https://api.jquery.com/jQuery.fn.extend/

 

标签:jQuery,Object,target,extend,object,will,properties
来源: https://www.cnblogs.com/chucklu/p/11101019.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有