123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>jQuery.extend浅拷贝代码实现</title>
</head>
<body>
    <div id="app">
        extend 用法:<br>
        jQuery.extend( target [, object1 ] [, objectN ] )        Returns: Object
        <br>
        <a href="https://api.jquery.com/jQuery.extend/" target="_blank">jQuery.extend API</a>
        <br>
        jQuery.extend( [deep ], target, object1 [, objectN ] )       Returns: Object
        <br>
        <a href="https://api.jquery.com/jQuery.extend/" target="_blank">jQuery.fn.extend API</a>
    </div>
<!--     <script src="./jquery-3.4.1.js">
    </script> -->
    <script src="https://unpkg.com/jquery@3.4.1/dist/jquery.js"></script>
    <script>
        // 浅拷贝实现
        $.extend = function(){
            // options 是扩展的对象object1,object2...
            var options,
            // object对象上的键
            name,
            // copy object对象上的值,也就是是需要拷贝的值
            copy,
            // 扩展目标对象,可能不是对象,所以或空对象
            target = arguments[0] || {},
            // 定义i为1
            i = 1,
            // 定义实参个数length
            length = arguments.length;
            // 只有一个参数时
            if(i === length){
                target = this;
                i--;
            }
            for(; i < length; i++){
                // 不是underfined 也不是null
                if((options = arguments[i]) !=  null){
                    for(name in options){
                        copy = options[name];
                        // 防止死循环,continue 跳出当前此次循环
                        if ( name === "__proto__" || target === copy ) {
                            continue;
                        }
                        if ( copy !== undefined ) {
                            target[ name ] = copy;
                        }
                    }
                }

            }
            // 最后返回目标对象
            return target;
        }
    </script>
    <script>
        //  extend 用法:
        // ```
        // jQuery.extend( target [, object1 ] [, objectN ] )

        // jQuery.extend( [deep ], target, object1 [, objectN ] )
        // ```

        // 1. jQuery.extend( target)
        var result1 = $.extend({
            job: '前端开发工程师',
        });

        console.log(result1, 'result1', result1.job); // $函数 加了一个属性 job  // 前端开发工程师

        // 2. jQuery.extend( target, object1)
        var result2 = $.extend({
            name: '若川',
        },
        {
            job: '前端开发工程师',
        });

        console.log(result2, 'result2'); // { name: '若川', job: '前端开发工程师' }

        // deep 深拷贝
        // 3. jQuery.extend( [deep ], target, object1 [, objectN ] )
        // var result3 = $.extend(true,  {
        //     name: '若川',
        //     other: {
        //         mac: 0,
        //         ubuntu: 1,
        //         windows: 1,
        //     },
        // }, {
        //     job: '前端开发工程师',
        //     other: {
        //         mac: 1,
        //         linux: 1,
        //         windows: 0,
        //     }
        // });
        // console.log(result3, 'result3');
        // deep true
        // {
        //     "name": "若川",
        //     "other": {
        //         "mac": 1,
        //         "ubuntu": 1,
        //         "windows": 0,
        //         "linux": 1
        //     },
        //     "job": "前端开发工程师"
        // }
        // deep false
        // {
        //     "name": "若川",
        //     "other": {
        //         "mac": 1,
        //         "linux": 1,
        //         "windows": 0
        //     },
        //     "job": "前端开发工程师"
        // }

    </script>
</body>
</html>

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console