JS中经常使用的封装函数4种方法:javascript
1. 函数封装法:css
function box(){ }
2. 封装成对象 :java
let Cookie = { get(){ }, set(){ } }
3. 封装成构造函数:数组
function Dog(){ }
4. 类的方法:函数
class Person{ constructor(){ } show(){ } }
jQuery中常见的封装函数方法:this
方法一:spa
$.extend({ log(s){ console.log(s)//封装了打印函数 } })
方法二:prototype
$.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(){ //数组上直接调用这个函数 })
欢迎你们多多交流,若有疑问能够在博客上问我哦~blog