Extend method in jquery

Source: Internet
Author: User

$.extend extends an object with one or more other objects, returning the object being extended. Parameter Type 1: Passing in only one object is an extension of the jquery tool method.
1     $ (function() {2        $.extend ({3             function(SMSG) {  4                console.log (SMSG); 5             }6        }); 7         $.console (' jquery ');//jquery8     });

Parameter Type 2: Passing in multiple objects is an extension of the first object.

1$(function(){2         varOtarget = {};3         4$.extend (Otarget, {name: ' Hum ')});5Console.log (Otarget.name);//Hum6         7$.extend (Otarget, {name: ' Hum '}, {age:20});8Console.log (Otarget.age);// -9         Ten$.extend (Otarget, {name: ' Hum '}, {name: ' Tyx ', age:20}); OneConsole.log (Otarget.name);//Tyx property with the same name will be overwritten A          -         //Note: The return value of $.extend () is an extended object -});

Parameter Type 3: The first parameter is a Boolean value, followed by multiple objects.

If the first argument is true, the copy is deep;

If the first argument is false, a shallow copy is performed (the default is a shallow copy), and the result is the same as when there is no Boolean argument, but the target object's value does not change (in other cases the target object and the returned result are the same object).

1$(function(){2         varOtarget = {courses: {math:100, english:80}};3Console.log ($.extend (otarget, {name: ' Hum ', courses: {math:50, chinese:99}}));//{name: ' Hum ', courses: {math:50, chinese:99}}4Console.log (Otarget);//{name: ' Hum ', courses: {math:50, chinese:99}}5 6         varOtarget = {courses: {math:100, english:80}};7Console.log ($.extend (false, Otarget, {name: ' Hum ', courses: {math:50, chinese:99}});//{name: ' Hum ', courses: {math:50, chinese:99}}8Console.log (Otarget);//{courses: {math:100, english:80}}9 Ten         varOtarget = {courses: {math:100, english:80}}; OneConsole.log ($.extend (true, Otarget, {name: ' Hum ', courses: {math:50, chinese:99, english:80}});//{name: ' Hum ', courses: {math:50, chinese:99, english:80}} AConsole.log (Otarget);//{name: ' Hum ', courses: {math:50, chinese:99, english:80}} -});

The difference between a deep copy and a shallow copy:

A deep copy causes all objects in the object to be recursively copied, and there is no referential relationship between the target object and the copied object;

A shallow copy does not allow the object to be recursively copied, and the object type properties of the target object and the layer within the copied object are referenced to each other.

1$(function(){2         varOtarget = {courses: {math:100, english:80}};3         varOcopy = {name: ' Hum ', courses: {math:50, chinese:99}};4Console.log ($.extend (Otarget, ocopy));//{name: ' Hum ', courses: {math:50, chinese:99}}5Console.log (Otarget);//{name: ' Hum ', courses: {math:50, chinese:99}}6Console.log (ocopy.courses = = otarget.courses);//True The Courses property of two objects points to the same object7OTarget.courses.chinese = 200;8Console.log (OCopy.courses.chinese);// $9 Ten         varOtarget = {courses: {math:100, english:80}}; One         varOcopy = {name: ' Hum ', courses: {math:50, chinese:99}}; AConsole.log ($.extend (true, Otarget, ocopy));//{name: ' Hum ', courses: {math:50, chinese:99, english:80}} -Console.log (Otarget);//{name: ' Hum ', courses: {math:50, chinese:99, english:80}} -Console.log (ocopy.courses = = otarget.courses);//False The Courses property of two objects no longer points to the same object theOTarget.courses.chinese = 200; -Console.log (OCopy.courses.chinese);// About -});

$.fn.extend when the method only passes an object, it is the extension to the $.fn. In other cases, as with the $.extend method, extend the object.
1 <Div>Content</Div>2 <Scriptsrc= "Jquery.js"></Script>3 <Script>4     $(function(){5 $.fn.extend ({6 ChangeColor:function(){7                 $( This). CSS ('Color', 'Blue');8             }9         });Ten         $('Div'). ChangeColor (); One     }); A </Script>

Extend method in jquery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.