<bdo id="ofhmm"><meter id="ofhmm"></meter></bdo>

<li id="ofhmm"><legend id="ofhmm"></legend></li>
<bdo id="ofhmm"><tbody id="ofhmm"></tbody></bdo>

<bdo id="ofhmm"></bdo>
<label id="ofhmm"></label>

    \n
    \n<\/body>
    \n<\/html>
    \n
    \n<\/div>\nWrite the usage in js below:

    \n<\/p>Merge two ordinary objects

    \n<\/p>\n

    <\/p>\n

    Copy code<\/u><\/a> The code is as follows:<\/span><\/div>\n
    \n\/\/Merging attributes for two ordinary objects
    \nvar obj1={name:'Tom',age:22};
    \nvar obj2={name:'Jack',height:180};
    \nconsole.log($.extend(obj1,obj2)); \/\/Object {name: \"Jack\", age: 22, height: 180}
    \n
    \n<\/div>\nAdd properties or methods to jQuery objects

    \n<\/p>\n

    <\/p>\n

    Copy code<\/u><\/a> The code is as follows:<\/span><\/div>\n
    \n$.extend({hehe:function(){alert('hehe');}});
    \n$.hehe(); \/\/alert('hehe')
    \n
    \n<\/div>\nThis usage is very important. It is the implementation method of adding instance properties and methods as well as prototype properties and methods inside jQuery. It is also the method of writing jQuery plug-ins. The following is the use of the extend method in jQuery 1.7.1 to extend its own methods and properties

    \n<\/p>\n

    <\/p>\n

    Copy code<\/u><\/a> The code is as follows:<\/span><\/div>\n
    \njQuery.extend({
    \nnoConflict: function( deep ) {
    \n??????????? if ( window.$ === jQuery ) {
    \n?????????????? window.$ = _$;
    \n}
    \nIf ( deep && window.jQuery === jQuery ) {
    \n?????????????? window.jQuery = _jQuery;
    \n}
    \n???????? return jQuery;
    \n},
    \n\/\/ Is the DOM ready to be used? Set to true once it occurs.
    \nisReady: false,
    \n\/\/ A counter to track how many items to wait for before
    \n\/\/ the ready event fires. See #6781
    \nreadyWait: 1,
    \n.....
    \n
    \n<\/div>\nIn this example, only one object parameter is passed in, so by default, this is regarded as the object to be merged and modified

    \n<\/p>Add properties or methods to jQuery object instances

    \n<\/p>\n

    <\/p>\n

    Copy code<\/u><\/a> The code is as follows:<\/span><\/div>\n
    \n\/\/Extended merging for jQuery instances
    \nconsole.log($('img').extend({'title':'img'}));\/\/[img, img#img.img, prevObject: jQuery.fn.jQuery.init[1], context : document, selector: \"img\", title: \"img\", constructor: function…]
    \n
    \n<\/div>\nOnly merge and do not modify the objects to be merged

    \n<\/p>\n

    <\/p>\n

    Copy code<\/u><\/a> The code is as follows:<\/span><\/div>\n
    \nvar obj1={name:'Tom',age:22};
    \nvar obj2={name:'Jack',height:180};
    \nconsole.log($.extend(obj1,obj2)); \/\/Object {name: \"Jack\", age: 22, height: 180}
    \nconsole.log(obj1); \/\/Object {name: \"Jack\", age: 22, height: 180}
    \n
    \n

    By default, the object to be merged is modified like the returned result. If you just want to get a merged object but do not want to destroy any of the original objects, you can use this method <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nvar obj1={name:'Tom',age:22};
    \nvar obj2={name:'Jack',height:180};
    \nvar empty={};
    \nconsole.log($.extend(empty,obj1,obj2)); \/\/Object {name: \"Jack\", age: 22, height: 180}
    \nconsole.log(obj1); \/\/Object {name: \"Tom\", age: 22}
    \n<\/div>\n

    If used, recursive merging or deep copy<\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nvar obj1={name:'Tom',love:{drink:'milk',eat:'bread'}};
    \nvar obj2={name:'Jack',love:{drink:'water',sport:'football'}};
    \nconsole.log(($.extend(false,obj1,obj2)).love); \/\/Object {drink: \"water\", sport: \"football\"}
    \nconsole.log(($.extend(true,obj1,obj2)).love); \/\/Object {drink: \"water\", eat: \"bread\", sport: \"football\"}
    \n<\/div>\n

    For detailed usage, please see the reference manual http:\/\/www.w3cschool.cc\/manual\/jquery\/<\/a><\/p>\n

    Let’s analyze how it is implemented in the 1.7.1 source code: <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \njQuery.extend = jQuery.fn.extend = function() {
    \nvar options, name, src, copy, copyIsArray, clone,
    \ntarget = arguments[0] || {},
    \ni = 1,
    \nlength = arguments.length,
    \ndeep = false;
    \n...
    \n}
    \n<\/div>\n

    First, a set of variables is defined. Since the number of parameters is uncertain, the arguments object is directly called to access the passed parameters <\/p>\n

    Variable options: points to a source object.
    \nVariable name: represents an attribute name of a source object.
    \nVariable src: Represents the original value of an attribute of the target object.
    \nVariable copy: represents the value of an attribute of a source object.
    \nVariable copyIsArray: Indicates whether the variable copy is an array.
    \nVariable clone: ??represents the correction value of the original value during deep copying.
    \nVariable target: points to the target object.
    \nVariable i: represents the starting index of the source object.
    \nVariable length: indicates the number of parameters and is used to modify the variable target.
    \nVariable deep: indicates whether to perform deep copy, the default is false. <\/p>\n

    In order to better understand the code implementation, here is an example given above as a demonstration to observe the execution of the source code<\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nvar obj1={name:'Tom',love:{drink:'milk',eat:'bread'}};
    \nvar obj2={name:'Jack',love:{drink:'water',sport:'football'}};
    \n$.extend(true,obj1,obj2)
    \n<\/div>\n

    Source code analysis<\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \n\/\/ Handle a deep copy situation
    \nIf ( typeof target === \"boolean\" ) {
    \ndeep = target;
    \ntarget = arguments[1] || {};
    \n\/\/ skip the boolean and the target
    \ni = 2;
    \n}
    \n<\/div>\n

    Determine whether it is a deep copy. If the first parameter is a Boolean value, then give the value of the first parameter to deep, and then use the second parameter as the target object. If the second parameter does not exist, assign it to one. Empty object, change the subscript of the source object to 2. In this example, it is done here because the first parameter is true and then deep is changed to true. The target is modified to the second parameter, which is obj1. The starting subscript of the source object is 2, which means starting from the third one as the source object, which is obj2<\/p> in this example.\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \n\/\/ Handle case when target is a string or something (possible in deep copy)
    \nIf ( typeof target !== \"object\" && !jQuery.isFunction(target) ) {
    \nTarget = {};
    \n}
    \n<\/div>\n

    The target is further processed here. Adding custom attributes is invalid for non-object and function data types. For example, strings can call their own methods and attributes<\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \n\/\/ extend jQuery itself if only one argument is passed
    \nIf ( length === i ) {
    \ntarget = this;
    \n????—i;
    \n}
    \n<\/div>\n

    If the length attribute is equal to the value of i, it means that there is no target object. Under normal circumstances, length should be greater than the value of i. Then use this as the target object at this time and reduce the i value by one to achieve the length value greater than the i value. (1 greater than i) <\/p>\n

    This is the implementation principle of jQuery’s method of extending attributes to itself, as long as the target object is not passed in <\/p>\n

    Two possible situations: $.extend(obj) or $.extend(false\/true,obj);<\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \n??? for ( ; i < length; i ) {
    \n??????? \/\/ Only deal with non-null\/undefined values
    \n??????? if ( (options = arguments[ i ]) != null ) {
    \n??????????? \/\/ Extend the base object
    \n??????????? for ( name in options ) {
    \n??????????????? src = target[ name ];
    \n??????????????? copy = options[ name ];
    \n??????????????? \/\/ Prevent never-ending loop
    \n??????????????? if ( target === copy ) {
    \n??????????????????? continue;
    \n??????????????? }
    \n??????????????? \/\/ Recurse if we're merging plain objects or arrays
    \n??????????????? if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
    \n??????????????????? if ( copyIsArray ) {
    \n??????????????????????? copyIsArray = false;
    \n??????????????????????? clone = src && jQuery.isArray(src) ? src : [];
    \n??????????????????? } else {
    \n??????????????????????? clone = src && jQuery.isPlainObject(src) ? src : {};
    \n??????????????????? }
    \n??????????????????? \/\/ Never move original objects, clone them
    \n??????????????????? target[ name ] = jQuery.extend( deep, clone, copy );
    \n??????????????? \/\/ Don't bring in undefined values
    \n??????????????? } else if ( copy !== undefined ) {
    \n??????????????????? target[ name ] = copy;
    \n??????????????? }
    \n??????????? }
    \n??????? }
    \n??? }
    \n<\/div>\n

    這個部分就是此方法的核心了,從arguements對象的第i個下標(biāo)值開始循環(huán)操作首先過濾掉源對象是null或者是undefined的情況可以看到其實<\/p>\n

    源對象不一定真的就是對像,也可以是其他類型的值比如字符串比如這樣寫:<\/p>\n

    <\/p>\n

    \n復(fù)制代碼<\/u><\/a><\/span> 代碼如下:<\/div>\n
    \n
    \nconsole.log($.extend({'name':'tom'},'aa'));?? \/\/Object {0: \"a\", 1: \"a\", name: \"tom\"}
    \n<\/div>\n

    是不是感覺很奇怪???究竟是怎么實現(xiàn)的呢?下面接著看<\/p>\n

    過濾完之后開始進(jìn)行for循環(huán) src保存的是目標(biāo)對象的某個鍵的值,copy屬性保存的源對象的某個鍵的值,這兩個鍵都是一樣的<\/p>\n

    <\/p>\n

    \n復(fù)制代碼<\/u><\/a><\/span> 代碼如下:<\/div>\n
    \n
    \n\/\/ Prevent never-ending loop
    \nIf ( target === copy ) {
    \n???????????????????????? continue;
    \n????????????????}
    \n<\/div>\n

    If a certain attribute value of the source object is the target object, it may cause an infinite loop and cause the program to crash, so a restriction is made here to allow it to skip this loop. For example: <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nvar o = {};
    \no.n1 = o;
    \n$.extend( true, o, { n2: o } );
    \n\/\/ throw exception:
    \n\/\/ Uncaught RangeError: Maximum call stack size exceeded
    \n<\/div>\n

    But doing so will also unfairly affect some normal situations such as: <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nvar obj1={a:'a'}
    \nvar obj2={a:obj1};
    \nconsole.log($.extend(obj1,obj2)); \/\/Object {a: \"a\"}
    \n<\/div>\n

    This situation also satisfies that the source object value is equal to the target object, but it turns out that the attribute value of a of obj1 has not been modified, because continue is executed. Below, comment out this paragraph in the source code before executing <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nObject {a: Object}
    \n<\/div>\n

    At this time, it has been modified normally. I personally feel that this area needs improvement; <\/p>\n

    Then there is an if judgment, which is to distinguish whether it is a deep copy. First, do not look at the deep copy and first look at the general <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \ntarget[ name ] = copy;
    \n<\/div>\n

    It is very simple. As long as the copy has a value, it is copied directly to the target object. If the target object has some modifications, it is added. In this way, the merge is achieved. <\/p>\n

    After the for loop, the new target object is returned, so the target object is finally modified, and the result is the same as the returned result. <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \n\/\/ Return the modified object
    \nReturn target;
    \n};
    \n<\/div>\n

    Let’s talk about how to handle deep copy<\/p>\n

    First ensure that deep is true, copy has a value and is an object or array (if it is not an object or array, deep copying is out of the question) and then it is processed by arrays and objects. Let’s look at the array first: <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nif ( copyIsArray ) {
    \n????????? copyIsArray = false;
    \n? ? ? clone = src && jQuery.isArray(src) ? src : [];\n

    } else {
    \n????clone = src && jQuery.isPlainObject(src) ? src: {};
    \n}
    \n<\/p>\n<\/div>\n

    If the value of the array copyIsArray is true, then go inside and change the value to false. For the source object attribute of the current loop, the target object may or may not have it. If it does, judge whether it is an array. If so, it is the original one. If the array is unchanged, let it become an array, because since the current attribute of the source object is the array, the last target element must also be an array. Either an array or an object. Change the current properties of the target object to an object. <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \n\/\/ Never move original objects, clone them
    \nTarget[ name ] = jQuery.extend( deep, clone, copy );
    \n<\/div>\n

    Then recursively merge the current attribute value of the source object (which is an array or object) and the current attribute of the modified target object and assign the returned new array or object to the target object, ultimately achieving deep copying. <\/p>\n

    But there is a rather strange phenomenon here, such as this: <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nconsole.log($.extend({a:1},'aa')); \/\/Object {0: \"a\", 1: \"a\", a: 1}
    \n<\/div>\n

    The original source object is not necessarily the object e, and the string can be split and merged with the target object. It turns out that the for...in loop operates on strings<\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nvar str='aa';
    \nfor(var name in str){
    \nconsole.log(name);
    \nconsole.log(str[name])
    \n}
    \n<\/div>\n

    This is also possible, it will split the string and read it according to the numerical subscript, but in the source code <\/p>\n

    <\/p>\n

    \nCopy code<\/u><\/a><\/span> The code is as follows:<\/div>\n
    \n
    \nif ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) )
    \n<\/div>\n

    is limited to arrays and objects, so will it have no effect during deep copying? <\/p>\n

    After my test, deep copying is also possible, because the copied value in the source code turned into an anonymous function <\/p>\n

    alert(jQuery.isPlainObject(copy)); \/\/true<\/p>\n

    As for why it is a function, I haven’t figured it out yet and will leave it to be solved later! <\/p>\n<\/div>"}

    国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

    Home Web Front-end JS Tutorial Detailed explanation of extend() and fn.extend() methods in jQuery_jquery

    Detailed explanation of extend() and fn.extend() methods in jQuery_jquery

    May 16, 2016 pm 03:56 PM
    jquery

    These two methods use the same code. One is used to merge properties and methods for jQuery objects or ordinary objects. The other is for instances of jQuery objects. Here are a few examples of basic usage:

    The html code is as follows:

    Copy code The code is as follows:





    ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????





    Write the usage in js below:

    Merge two ordinary objects

    Copy code The code is as follows:
    //Merging attributes for two ordinary objects
    var obj1={name:'Tom',age:22};
    var obj2={name:'Jack',height:180};
    console.log($.extend(obj1,obj2)); //Object {name: "Jack", age: 22, height: 180}

    Add properties or methods to jQuery objects

    Copy code The code is as follows:
    $.extend({hehe:function(){alert('hehe');}});
    $.hehe(); //alert('hehe')

    This usage is very important. It is the implementation method of adding instance properties and methods as well as prototype properties and methods inside jQuery. It is also the method of writing jQuery plug-ins. The following is the use of the extend method in jQuery 1.7.1 to extend its own methods and properties

    Copy code The code is as follows:
    jQuery.extend({
    noConflict: function( deep ) {
    ??????????? if ( window.$ === jQuery ) {
    ?????????????? window.$ = _$;
    }
    If ( deep && window.jQuery === jQuery ) {
    ?????????????? window.jQuery = _jQuery;
    }
    ???????? return jQuery;
    },
    // Is the DOM ready to be used? Set to true once it occurs.
    isReady: false,
    // A counter to track how many items to wait for before
    // the ready event fires. See #6781
    readyWait: 1,
    .....

    In this example, only one object parameter is passed in, so by default, this is regarded as the object to be merged and modified

    Add properties or methods to jQuery object instances

    Copy code The code is as follows:
    //Extended merging for jQuery instances
    console.log($('img').extend({'title':'img'}));//[img, img#img.img, prevObject: jQuery.fn.jQuery.init[1], context : document, selector: "img", title: "img", constructor: function…]

    Only merge and do not modify the objects to be merged

    Copy code The code is as follows:
    var obj1={name:'Tom',age:22};
    var obj2={name:'Jack',height:180};
    console.log($.extend(obj1,obj2)); //Object {name: "Jack", age: 22, height: 180}
    console.log(obj1); //Object {name: "Jack", age: 22, height: 180}

    By default, the object to be merged is modified like the returned result. If you just want to get a merged object but do not want to destroy any of the original objects, you can use this method

    Copy code The code is as follows:

    var obj1={name:'Tom',age:22};
    var obj2={name:'Jack',height:180};
    var empty={};
    console.log($.extend(empty,obj1,obj2)); //Object {name: "Jack", age: 22, height: 180}
    console.log(obj1); //Object {name: "Tom", age: 22}

    If used, recursive merging or deep copy

    Copy code The code is as follows:

    var obj1={name:'Tom',love:{drink:'milk',eat:'bread'}};
    var obj2={name:'Jack',love:{drink:'water',sport:'football'}};
    console.log(($.extend(false,obj1,obj2)).love); //Object {drink: "water", sport: "football"}
    console.log(($.extend(true,obj1,obj2)).love); //Object {drink: "water", eat: "bread", sport: "football"}

    For detailed usage, please see the reference manual http://www.w3cschool.cc/manual/jquery/

    Let’s analyze how it is implemented in the 1.7.1 source code:

    Copy code The code is as follows:

    jQuery.extend = jQuery.fn.extend = function() {
    var options, name, src, copy, copyIsArray, clone,
    target = arguments[0] || {},
    i = 1,
    length = arguments.length,
    deep = false;
    ...
    }

    First, a set of variables is defined. Since the number of parameters is uncertain, the arguments object is directly called to access the passed parameters

    Variable options: points to a source object.
    Variable name: represents an attribute name of a source object.
    Variable src: Represents the original value of an attribute of the target object.
    Variable copy: represents the value of an attribute of a source object.
    Variable copyIsArray: Indicates whether the variable copy is an array.
    Variable clone: ??represents the correction value of the original value during deep copying.
    Variable target: points to the target object.
    Variable i: represents the starting index of the source object.
    Variable length: indicates the number of parameters and is used to modify the variable target.
    Variable deep: indicates whether to perform deep copy, the default is false.

    In order to better understand the code implementation, here is an example given above as a demonstration to observe the execution of the source code

    Copy code The code is as follows:

    var obj1={name:'Tom',love:{drink:'milk',eat:'bread'}};
    var obj2={name:'Jack',love:{drink:'water',sport:'football'}};
    $.extend(true,obj1,obj2)

    Source code analysis

    Copy code The code is as follows:

    // Handle a deep copy situation
    If ( typeof target === "boolean" ) {
    deep = target;
    target = arguments[1] || {};
    // skip the boolean and the target
    i = 2;
    }

    Determine whether it is a deep copy. If the first parameter is a Boolean value, then give the value of the first parameter to deep, and then use the second parameter as the target object. If the second parameter does not exist, assign it to one. Empty object, change the subscript of the source object to 2. In this example, it is done here because the first parameter is true and then deep is changed to true. The target is modified to the second parameter, which is obj1. The starting subscript of the source object is 2, which means starting from the third one as the source object, which is obj2

    in this example.

    Copy code The code is as follows:

    // Handle case when target is a string or something (possible in deep copy)
    If ( typeof target !== "object" && !jQuery.isFunction(target) ) {
    Target = {};
    }

    The target is further processed here. Adding custom attributes is invalid for non-object and function data types. For example, strings can call their own methods and attributes

    Copy code The code is as follows:

    // extend jQuery itself if only one argument is passed
    If ( length === i ) {
    target = this;
    ????—i;
    }

    If the length attribute is equal to the value of i, it means that there is no target object. Under normal circumstances, length should be greater than the value of i. Then use this as the target object at this time and reduce the i value by one to achieve the length value greater than the i value. (1 greater than i)

    This is the implementation principle of jQuery’s method of extending attributes to itself, as long as the target object is not passed in

    Two possible situations: $.extend(obj) or $.extend(false/true,obj);

    Copy code The code is as follows:

    ??? for ( ; i < length; i ) {
    ??????? // Only deal with non-null/undefined values
    ??????? if ( (options = arguments[ i ]) != null ) {
    ??????????? // Extend the base object
    ??????????? for ( name in options ) {
    ??????????????? src = target[ name ];
    ??????????????? copy = options[ name ];
    ??????????????? // Prevent never-ending loop
    ??????????????? if ( target === copy ) {
    ??????????????????? continue;
    ??????????????? }
    ??????????????? // Recurse if we're merging plain objects or arrays
    ??????????????? if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) {
    ??????????????????? if ( copyIsArray ) {
    ??????????????????????? copyIsArray = false;
    ??????????????????????? clone = src && jQuery.isArray(src) ? src : [];
    ??????????????????? } else {
    ??????????????????????? clone = src && jQuery.isPlainObject(src) ? src : {};
    ??????????????????? }
    ??????????????????? // Never move original objects, clone them
    ??????????????????? target[ name ] = jQuery.extend( deep, clone, copy );
    ??????????????? // Don't bring in undefined values
    ??????????????? } else if ( copy !== undefined ) {
    ??????????????????? target[ name ] = copy;
    ??????????????? }
    ??????????? }
    ??????? }
    ??? }

    這個部分就是此方法的核心了,從arguements對象的第i個下標(biāo)值開始循環(huán)操作首先過濾掉源對象是null或者是undefined的情況可以看到其實

    源對象不一定真的就是對像,也可以是其他類型的值比如字符串比如這樣寫:

    復(fù)制代碼 代碼如下:

    console.log($.extend({'name':'tom'},'aa'));?? //Object {0: "a", 1: "a", name: "tom"}

    是不是感覺很奇怪???究竟是怎么實現(xiàn)的呢?下面接著看

    過濾完之后開始進(jìn)行for循環(huán) src保存的是目標(biāo)對象的某個鍵的值,copy屬性保存的源對象的某個鍵的值,這兩個鍵都是一樣的

    復(fù)制代碼 代碼如下:

    // Prevent never-ending loop
    If ( target === copy ) {
    ???????????????????????? continue;
    ????????????????}

    If a certain attribute value of the source object is the target object, it may cause an infinite loop and cause the program to crash, so a restriction is made here to allow it to skip this loop. For example:

    Copy code The code is as follows:

    var o = {};
    o.n1 = o;
    $.extend( true, o, { n2: o } );
    // throw exception:
    // Uncaught RangeError: Maximum call stack size exceeded

    But doing so will also unfairly affect some normal situations such as:

    Copy code The code is as follows:

    var obj1={a:'a'}
    var obj2={a:obj1};
    console.log($.extend(obj1,obj2)); //Object {a: "a"}

    This situation also satisfies that the source object value is equal to the target object, but it turns out that the attribute value of a of obj1 has not been modified, because continue is executed. Below, comment out this paragraph in the source code before executing

    Copy code The code is as follows:

    Object {a: Object}

    At this time, it has been modified normally. I personally feel that this area needs improvement;

    Then there is an if judgment, which is to distinguish whether it is a deep copy. First, do not look at the deep copy and first look at the general

    Copy code The code is as follows:

    target[ name ] = copy;

    It is very simple. As long as the copy has a value, it is copied directly to the target object. If the target object has some modifications, it is added. In this way, the merge is achieved.

    After the for loop, the new target object is returned, so the target object is finally modified, and the result is the same as the returned result.

    Copy code The code is as follows:

    // Return the modified object
    Return target;
    };

    Let’s talk about how to handle deep copy

    First ensure that deep is true, copy has a value and is an object or array (if it is not an object or array, deep copying is out of the question) and then it is processed by arrays and objects. Let’s look at the array first:

    Copy code The code is as follows:

    if ( copyIsArray ) {
    ????????? copyIsArray = false;
    ? ? ? clone = src && jQuery.isArray(src) ? src : [];

    } else {
    ????clone = src && jQuery.isPlainObject(src) ? src: {};
    }

    If the value of the array copyIsArray is true, then go inside and change the value to false. For the source object attribute of the current loop, the target object may or may not have it. If it does, judge whether it is an array. If so, it is the original one. If the array is unchanged, let it become an array, because since the current attribute of the source object is the array, the last target element must also be an array. Either an array or an object. Change the current properties of the target object to an object.

    Copy code The code is as follows:

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

    Then recursively merge the current attribute value of the source object (which is an array or object) and the current attribute of the modified target object and assign the returned new array or object to the target object, ultimately achieving deep copying.

    But there is a rather strange phenomenon here, such as this:

    Copy code The code is as follows:

    console.log($.extend({a:1},'aa')); //Object {0: "a", 1: "a", a: 1}

    The original source object is not necessarily the object e, and the string can be split and merged with the target object. It turns out that the for...in loop operates on strings

    Copy code The code is as follows:

    var str='aa';
    for(var name in str){
    console.log(name);
    console.log(str[name])
    }

    This is also possible, it will split the string and read it according to the numerical subscript, but in the source code

    Copy code The code is as follows:

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

    is limited to arrays and objects, so will it have no effect during deep copying?

    After my test, deep copying is also possible, because the copied value in the source code turned into an anonymous function

    alert(jQuery.isPlainObject(copy)); //true

    As for why it is a function, I haven’t figured it out yet and will leave it to be solved later!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    PHP Tutorial
    1502
    276
    Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

    Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

    How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

    How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

    jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

    Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

    Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

    Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ??the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

    How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

    How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

    Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

    jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

    Introduction to how to add new rows to a table using jQuery Introduction to how to add new rows to a table using jQuery Feb 29, 2024 am 08:12 AM

    jQuery is a popular JavaScript library widely used in web development. During web development, it is often necessary to dynamically add new rows to tables through JavaScript. This article will introduce how to use jQuery to add new rows to a table, and provide specific code examples. First, we need to introduce the jQuery library into the HTML page. The jQuery library can be introduced in the tag through the following code:

    How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

    How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

    See all articles