Home Web Front-end JS Tutorial Analysis of jquery's extend method

Analysis of jquery's extend method

Oct 12, 2017 am 10:26 AM
extend jquery parse

1. Some time ago, I have been busy studying data visualization (d3.js, three.js) and the laravel framework of PHP. I have also encountered many things in life. This is probably a confused period in life.

Looking back, when I first started working, my goal was very clear, what to learn. What kind of heights do you want to reach in the future? You need to sit side by side with the great gods and talk about everything. We have been

working hard towards this goal. But as I saw and did more things, I became more and more confused - I felt like I knew everything. With Du Niang, I basically wouldn't encounter any problems at work.

But if you think about it carefully, you know nothing. When I write with my eyes closed, I can’t remember this or that (maybe it’s because of my age). The goal has not changed, but how can I become my idol, but I am confused.

Drifting and groping alone in the ocean at the front end, I know that I must have taken many detours, but I also know that I keep moving forward. There is a lack of a bright light in the process of moving forward. How can we find our own bright light?

When I was bored, I read the source code of jQuery, hoping to find my own light.


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

/**

    1. $.extend的用法。 第一个参数是决定是否需要深复制。 由 true, false。 默认是浅复制

    params:

    options => 接收传递过来的arguments 的中间参数。

    name =>  没对对象的key值。

    src =>   当传递的对象key值相同到时候。要和并

    copy =>   复制的value 值。

    copyIsArray =>  判断value对象是不是数组。

    clone =>    当深度复制的时候。需要新建一个变量。不会改变原始的值。

    target =>  最开始是默认值是取传进来的第一个参数。过后往后一直跳。$.extend(a,b,c); target 为 a, 为b,为c。

    i =>  决定当前target 等于参数中的某个值

    length =>

    deep => 默认是false 是决定是否需要深复制的参数。 true 是深复制。 false 是浅复制*/jQuery.extend = jQuery.fn.extend = function() {    var options, name, src, copy, copyIsArray, clone,

        target = arguments[ 0 ] || {},

        i = 1,

        length = arguments.length,

        deep = false;    // 判断 target 来决定是否 深复制

    if ( typeof target === "boolean" ) {

        deep = target;        //当深复制的时候,需要跳过第一个参数。 来循环传进来的参数。 刚开始 1,2,3

        target = arguments[ i ] || {};

        i++;

         

    }    // 当 target 不是一个对象且 不是一个函数对象 这个时候就把target 变为空对象。

    if ( typeof target !== "object" && !jQuery.isFunction( target ) ) {

        target = {};

    }    // 如果传递过来的参数只有一个的情况下。target 就是该参数。

    if ( i === length ) {

        target = this;

        i--;  // 把i 改为1    }    for ( ; i < length; i++ ) {        // 处理非空 或者 undefined 的情况。   null == null 。 undefined == null;

        //options  是来接收除了第一个参数外的所有参数。中间桥梁的作用吧

        if ( ( options = arguments[ i ] ) != null ) {            // name in options 决定了 传过来的值必须是对象。如果不是对象的话。将要被拆分

            for ( name in options ) {                //这个是决定后面同级 的key 值, 后面将要覆盖前面的。

                src = target[ name ];                //获取 key值为 name 的 value值。

                copy = options[ name ];                // Prevent never-ending loop 阻止无限循环。 ???没有搞懂,设么情况下才会出现这种情况。

 

                if ( target === copy ) {                   

                continue;

                }                /**

 

                1.深复制,且value 为对象属性的时候执行递归复制。 isPlainObect(copy) 判断copy第不是一个 纯对象

                2.copy 时一个数组的时候。执行里面的函数。

                3.(因为只有对象value 和 数组value 是需要进行深复制的。)                */

                if ( deep && copy && ( jQuery.isPlainObject( copy ) ||

                    ( copyIsArray = Array.isArray( copy ) ) ) ) {                   

                    if ( copyIsArray ) {

                         

                        copyIsArray = false;

                        clone = src && Array.isArray( src ) ? src : [];

 

                    } else {

                         

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

                    }                    // 创建新对象。来进行深度复制。

                    target[ name ] = jQuery.extend( deep, clone, copy );                //除了对象数组,和undefined 的值。

                } else if ( copy !== undefined ) {

                    target[ name ] = copy;

                }

            }

        }

    }    // Return the modified object

    return target;

};

Copy after login

This paragraph comes from jquery. version = version 3.2.1. Everyone's point of view must be different. I hope you can point out any mistakes. study together.

There is something in the above code that I have never figured out.


1

2

3

4

5

// Prevent never-ending loop 阻止无限循环。 ???没有搞懂,设么情况下才会出现这种情况。 {} != {} 没有想出来什么情况下才会出现target === copy的情况。

 

               if ( target === copy ) {                   

               continue;

               }

Copy after login

I’ve watched about 1/10 of it so far and learned a lot of tricks. Maybe you can really find your own light.

The above is the detailed content of Analysis of jquery's extend method. For more information, please follow other related articles on the PHP Chinese website!

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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)

Detailed explanation of Oracle error 3114: How to solve it quickly Detailed explanation of Oracle error 3114: How to solve it quickly Mar 08, 2024 pm 02:42 PM

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

Parsing Wormhole NTT: an open framework for any Token Parsing Wormhole NTT: an open framework for any Token Mar 05, 2024 pm 12:46 PM

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

Analysis of the meaning and usage of midpoint in PHP Analysis of the meaning and usage of midpoint in PHP Mar 27, 2024 pm 08:57 PM

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

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

Analysis of new features of Win11: How to skip logging in to Microsoft account Analysis of new features of Win11: How to skip logging in to Microsoft account Mar 27, 2024 pm 05:24 PM

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

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:

Apache2 cannot correctly parse PHP files Apache2 cannot correctly parse PHP files Mar 08, 2024 am 11:09 AM

Due to space limitations, the following is a brief article: Apache2 is a commonly used web server software, and PHP is a widely used server-side scripting language. In the process of building a website, sometimes you encounter the problem that Apache2 cannot correctly parse the PHP file, causing the PHP code to fail to execute. This problem is usually caused by Apache2 not configuring the PHP module correctly, or the PHP module being incompatible with the version of Apache2. There are generally two ways to solve this problem, one is

Comparison of Java libraries for XML parsing: Finding the best solution Comparison of Java libraries for XML parsing: Finding the best solution Mar 09, 2024 am 09:10 AM

Introduction XML (Extensible Markup Language) is a popular format for storing and transmitting data. Parsing XML in Java is a necessary task for many applications, from data exchange to document processing. To parse XML efficiently, developers can use various Java libraries. This article will compare some of the most popular XML parsing libraries, focusing on their features, functionality, and performance to help developers make an informed choice. DOM (Document Object Model) parsing library JavaXMLDOMAPI: a standard DOM implementation provided by Oracle. It provides an object model that allows developers to access and manipulate XML documents. DocumentBuilderFactoryfactory=D

See all articles