Extension of jQuery object --extend
Anyone who has written a jquery plug-in knows that the jquery object can be extended through the extend provided by jquery, and this method can not only extend the jquery object, but also add new properties and methods to an object. This will be discussed later. introduce.
The methods of calling extend in different ways are also different:
What is extended through $.extend() is a static method;
What is extended through $.fn.extend() is the instance method.
Anyone who has written jQuery plug-ins should know that many times we use extend to add plug-ins to jQuery objects.
How to write the plug-in:
;(function($){ $.fn.extend({ Firstplus: function() {} }); //这样写的话插件的使用方法就是:$('div').Firstplus(); $.extend({ Secondplus: function() {} }); //这样写的话插件的使用方法就是:$.Secondplus();})($);
$.extend() and $.fn.extend() actually call the same function, so why are the functions they implement different?
jQuery.extend = jQuery.fn.extend = function() {} //源码285行
Mainly because this method extends the incoming object to this.
$.extend(xx) this points to the jQuery object, so what is extended at this time is the jQuery object, which can be called directly through $.xx.
$.fn.extend(xx) this points to the prototyep of the jQuery object, so the prototype of the extended jQuery object at this time and the instantiated jQuery object can call all methods on the jQuyer prototype. Call directly through $().xx.
The following are three different uses of extend:
1.jQuery.extend( [ object ] )
>将传入的 object 扩展到 this 对象上
2.jQuery.extend( target, [ object1 ,. .. objectN ] )
>将后面传入的 object1 到 objectN 扩展到 target 对象上
3.jQuery.extend( [ deep ], target, [object1,... objectN ] )
>如果传入了 deep 参数表示递归后面传入object1到objectN对象然后在扩展到target,这样同名的属性名就不会被覆盖了。
Look at the source code analysis in detail:
jQuery.extend = jQuery.fn.extend = function() {//定义了一些变量var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {}, //target用来存储传入的第一个对象(目标对象) //这个target不仅表示要进行合并的目标对象,也可以表示要扩展到jquery上的对象 i = 1, //i用来表示target后面传入的对象是arguments的第几个参数 length = arguments.length, deep = false; //deep变量表示,是否进行深度拷贝//先进行了一系列的if判断,来初始化参数,判断到底是要扩展jQuery还是对传入的对象进行扩展//如果第一个参数是布尔类型,则表示是否深度拷贝if ( typeof target === "boolean" ) { deep = target; target = arguments[1] || {}; //将目标对象置为传入的第二个参数,如果没有置为空对象 // skip the boolean and the target i = 2; //将i置为2}//如果target不是一个对象,将其置为空对象if ( typeof target !== "object" && !jQuery.isFunction(target) ) { target = {}; }//如果arguments和i相等,表示要扩展的jquery对象,让target指向this//这个this具体指向什么之前已经探讨过了if ( length === i ) { target = this; --i; //且让i--,这时arguments[i]表示的才是要扩展到jquery上的对象}for ( ; i < length; i++ ) { //开始遍历传入的 arguments // 只有参数不为空时才执行后面的操作 if ( (options = arguments[ i ]) != null ) { // 对对象进行扩展,无论传入的target对象,还是jQuery对象,都使用同样的方法进行扩展 for ( name in options ) { //遍历传入的对象的属性 src = target[ name ]; copy = options[ name ]; //防止target和obj的某个属性指向的是同一对象进入死循环 if ( target === copy ) { continue; } //是否进行深度拷贝 //这里说的深度拷贝,和我们平时说的深度拷贝有点区别;这里指的当obj的某个属性是一个对象时,是否对这个属性继续遍历,如果不进行遍历的话,会直接覆盖target对象的同名属性 if ( deep && copy && ( jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)) ) ) { if ( copyIsArray ) { //如果要拷贝的obj属性为数组 copyIsArray = false; clone = src && jQuery.isArray(src) ? src : []; } else { //如果要拷贝的obj属性为对象 clone = src && jQuery.isPlainObject(src) ? src : {}; } // 进行递归,直到属性不再为一个对象或数组 target[ name ] = jQuery.extend( deep, clone, copy ); } else if ( copy !== undefined ) {//如果不进行深拷贝且当前obj的属性不为空 //扩展target对象,且和当前obj属性名相同。 target[ name ] = copy; } } } }return target; 最后返回target对象,如果扩展的jquery对象,则返回的就是jquery对象 };
Through the extend function, we can see that the extend function implements many different functions through many if judgments:
Expand multiple objects to one object;
Expand multiple objects into one object, and traverse the objects to be expanded to prevent objects with the same attribute name from being overwritten;
Expand one Object to the jquery object;
Expand an object to the jquery prototype object.
You can also see later that jq has extended many tool methods to jQuery objects through this method. It has to be said that the structure of jq is still very compact. A method is not only provided for external use,
is also used internally many times
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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? 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 is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

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

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: <

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:

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

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
