$.extend应用

120 阅读1分钟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script src="./jquery-1.12.4.js"></script>
    <script>
        /* 这是一个插件 */
        function fn(obj){
            /* 这些是这个插件的默认功能 */
            let o = {
                name:'zhangsan',
                age:18,
                house:{
                    tpye:'北京四合院',
                    value:'1kw'
                }
            }
            /* 用extend来扩展插件的功能 */
            /* true深度合并 */
            $.extend(true,o,obj);
            console.log(o);
            for(var key in o){
                console.log(o[key])
            }
        } 
        fn({ car:'bmw',nv:'lili',house:{tpye:'别墅'} });  
    </script>

</body>
</html>