本文介绍了jQuery的extend方法,用于扩展jQuery对象和原型,实现对象和数组的深浅合并。通过$.extend()进行浅合并,$.extend(true,)执行深合并。此外,还探讨了浅克隆和深克隆的区别,提供了实现这两种克隆方式的示例。"
20011657,2380261,C++ Primer 第5版:初阶练习解析,"['C++', '编程基础', '循环结构', '输入/输出', '头文件']
摘要由CSDN通过智能技术生成
jQuery中的extend方法的作用
1.给jQuery的原型和对象扩展方法
2.数组和对象的深浅合并
jQuery.extend = jQuery.fn.extend=function(){
var options, name, src, copy, copyIsArray, clone,
target = arguments[0]||{
},
i =1,
length = arguments.length,
deep =false;// Handle a deep copy situationif(typeof target ==="boolean"){
deep = target;// Skip the boolean and the target
target = arguments[i]||{
};
i++;}// Handle case when target is a string or something (possible in deep copy)if(typeof target !=="object"&&!isFunction(target)){
target ={
};}// Extend jQuery itself if only one argument is passedif(i === length){
target =this;
i--;}for(; i < length; i++){
// Only deal with non-null/undefined valuesif((options = arguments[i])!=null){
// Extend the base objectfor(name in options){
copy = options[name];// Prevent Object.prototype pollution// Prevent never-ending loopif(name ==="__proto__"|| target === copy){
continue;}