Home > Web Front-end > JS Tutorial > Comparison and use of clone() and extend() in jQuery

Comparison and use of clone() and extend() in jQuery

巴扎黑
Release: 2017-06-25 10:03:01
Original
1585 people have browsed it

jQuery's clone() method, when I customize an object, for example

 var obj = {
    chart: {
        type: 'spline',
        inverted: true
    },
    title: {//标题文字
        text: 'Atmosphere Temperature by Altitude'
    },
   ...

};
Copy after login

When I want to customize another obj2 and copy the content of obj, I Using

 var obj2 = obj.clone();
Copy after login

obviously shows that I ignored that obj is just a object and does not have the clone() method. So it's obviously a wrong demonstration. However, the clone() method that comes with jQuery can no longer meet my needs this time, which is to copy one object to another object, clone(obj);

Shallow-level copy: for non-basicData type, after cloning, the two pointers point to the same memory space. In this case, if one object is operated on, the content of the other object will also change.

Deep copy: Copy every attribute and method of the pointed object to the object pointed to by the new object, just like a copy, so that the new object can be modified or otherwise The operation has no impact on the original object.

Below I give a deep copy method:

var cloneObj = function(obj){
var str, newobj = obj.constructor === Array ? [] : {};
if(typeof obj !== 'object'&& typeof obj !=='function'){
    return;
}else {
    for(var i in obj){
        newobj[i] = typeof obj[i] === 'object' ?
            cloneObj(obj[i]) : obj[i];
    }
}
return newobj;
};

//克隆
var obj2=clone(obj);
Copy after login

When I want to change some parameters inside, I can use the following method

obj2.title.text = "XXX";
Copy after login

extend():

Use the extend() method: Extension means that one or more properties can be extended to an object
to form a union effect. If we are talking about empty objects here, Expanding to an object is equivalent to cloning

$.extend(true,obj,{});
Copy after login

The code to obtain the union effect is as follows

 obj2 = $.extend(true,obj,obj2);
Copy after login

When you want to overwrite a parameter, you can choose the same override method as clone(), You can also choose to add the following code in front of

obj2 = $.extend(true,obj,obj2);

:

 var obj2 = {
       title: {
            text: 'XXX'
        },
        ...
        }
Copy after login

The above is the detailed content of Comparison and use of clone() and extend() in jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template