JS和JQUERY常见函数封装方式

JS中常用的封装函数4种方法

1. 函数封装法

function box(){

               }

2. 封装成对象 :

let Cookie = {

            get(){

                   },

            set(){

                   }

               }

3. 封装成构造函数:

function Dog(){

               }  

4. 类的方法

class Person{

                  constructor(){

            }

            show(){

            }

             }

 

jQuery中常见的封装函数方法:

方法一:

$.extend({

    log(s){

        console.log(s)//封装了打印函数

    }

})

方法二:

$.fn.mytest = function(){

            console.log(this,1111)//this 伪数组

            this.css({color:'yellow'})

 

        }

 

        $('div').mytest()//指出哪个选择器调用这个函数

方法三:也可以在原型函数上添加,例如:

var arr = [1,2,3]

        // arr.map(function(){})

        Array.prototype.myMap = function(fn){    //原型上添加

        }

        arr.myMap(function(){                    //数组上直接调用这个函数

        })

 

欢迎大家多多交流,如有疑问可以在博客上问我哦~

posted @ 2019-07-24 08:57  Rubchinskiy  阅读(3692)  评论(0编辑  收藏  举报