Read jQuery Part 2 (Two Extensions)_jquery
As follows
jQuery.extend = jQuery.fn.extend = function () {
...
};
We can use $.extend to extend custom objects, such as
var myself = {name:jack};
$.extend(myself, {setName: function(n) {this.name=n;} });
myself.setName("tom");
The setName method is added to the object myself through $.extend. But here we mainly discuss how $.extend builds the jQuery library. I didn’t know that jQuery.extend and jQuery.fn.extend in the above code are the same function. We browsed the jQuery library and found that some methods are extended by calling jQuery.extend, and some are extended by calling jQuery.fn.extend.
Discussed separately below:
1. Extension through jQuery.extend
We know that the jQuery type in jQuery.extend is function, that is, the typeof jQuery value is the string "function". If jQuery is regarded as a class, jQuery.extend is equivalent to adding the static method extend to the class. Static methods do not require a new instance to be called. They are called directly through "class name + method name". That is, jQuery.extend(...), jQuery is assigned to $. Therefore we are more accustomed to $.extend(...).
The jQuery.extend method is called directly in the source code and only passes one parameter. As follows
jQuery.extend({
noConflict: function ( deep ) {
window.$ = _$;
if ( deep )
window.jQuery = _jQuery;
return jQuery;
},
...
});
We know that if only one parameter is passed in extend, then this sentence will be executed
if ( length === i ) {
target = this;
--i;
}
That is, extend yourself, and this here is function jQuery. That is to say, many static methods have been added to function jQuery. These methods can be called directly through jQuery.xx (or $.xx) instead of executing (calling) the jQuery method first and then calling xx, such as $("# id").xx. Maybe the following example is easier to understand
function fun(){ }//Define a class (function)
//Add a static method extend
fun.extend=function(obj){
for(var a in obj)
this[a] = obj[a];//Note: tihs here is fun
}
//Calling extend adds a static attribute name to the class, and the static method method1
fun.extend({ name:"fun",method1:function(){}});
//Output name, prototype, extend, method1
console.dir(fun)
Therefore, jQuery isFunction, isArray, isWindow, etc. are all static methods and can only be referenced through $.isFunction, $.isArray, $.Window. It cannot be referenced through $("...").isFuction, $("...").isArray, $("...").isWindow.
2, extend through jQuery.fn.extend
jQuery.fn is equal to jQuery.prototype, which means that an extend method is attached to the prototype of function jQuery. When extending by calling jQuery.fn.extend(object) (note that only one parameter object is passed), the extend function will still execute
if ( length === i ) {
target = this;
--i;
}
At this time, this is jQuery.prototype (the first one mentioned is the jQuery function itself). That is, many attributes and methods are added to jQuery.prototype. When a jQuery function is executed, such as $() or jQuery(), more often $(). This function will create a new jQuery (see the previous article about the composition of jQuery objects). At this time, the extended attributes and methods are attached to the object generated by new. Maybe the following example is easier to understand
function fun(){}//Define a class (function)
//Add a method extended to the prototype of the class
fun.prototype.extend = function(obj){
for(var a in obj)
this[a] = obj[a];//Note: this here is fun.prototype
}
//Call the extend method on fun.prototype Add attributes, method
fun.prototype.extend({name:"fun2",method1:function(){}})
//Output name,extend,method1
console.dir(new fun( ))
So the extended properties or methods are added to the jQuery object. For example, bind, one, unbind, etc. can be called through $("...").bind, $("...").one, $("...").unbind. But it cannot be called through $.bind, $.one, $.unbind.
jQuery, like Prototype, extends the entire library through the extend method. Relatively speaking, jQuery's extension method is more difficult to understand.
The summary is as follows:
1. jQuery.extend({...}) is to add static properties or methods to function jQuery.
2. jQuery().extend({...}) is to add properties or methods to the jQuery object.
/js/2011/zchain/zchain-0.2.js

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

AI Hentai Generator
Generate AI Hentai for free.

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



To extend PHP function functionality, you can use extensions and third-party modules. Extensions provide additional functions and classes that can be installed and enabled through the pecl package manager. Third-party modules provide specific functionality and can be installed through the Composer package manager. Practical examples include using extensions to parse complex JSON data and using modules to validate data.

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

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

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:

PHP function return value types can be expressed as type description syntax, which clearly specifies the return value type of each function. Understanding return value types is critical to creating extensions that are compatible with the PHP core engine, avoiding unexpected conversions, improving efficiency, and enhancing code readability. Specifically, extension functions can define a return value type so that the PHP engine can optimize code execution based on that type and allow developers to explicitly handle the return value. In practice, extension functions can return PHP objects, and PHP code can handle the returned results according to the return value type.

Laravel is a popular PHP development framework with rich functions and flexible scalability. The Redis extension is a commonly used database caching tool. This article will deeply explore the use of Redis extensions in Laravel, introduce its basic concepts, configuration methods and specific code examples in detail to help developers better use Redis extensions to improve system performance. 1. What is RedisRedis is an open source memory data storage system, also known as

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
