函数封装
//cookie相关操作
var cookie={
  set:function(name, value, day) {
    let oDate = new Date()
    oDate.setDate(oDate.getDate() + (day || 30))
    document.cookie = name + '=' + value + ';expires=' + oDate + "; path=/;"
  },
  get:function(name) {
    let str = document.cookie
    let arr = str.split('; ')
    for (let i = 0; i < arr.length; i++) {
      let newArr = arr[i].split('=')
      if (newArr[0] === name) {
        return newArr[1]
      }
    }
  },
  del:function(name) {
    this.set(name, '', -1)
  },
 };

cookie.set('dyh','内容2',2);
console.log(cookie.get('dyh'));
cookie.del('dyh')

闭包方式
;(function() {

//cookie相关操作
const cookie={
  set:function(name, value, day) {
    let oDate = new Date()
    oDate.setDate(oDate.getDate() + (day || 30))
    document.cookie = name + '=' + value + ';expires=' + oDate + "; path=/;"
  },
  get:function(name) {
    let str = document.cookie
    let arr = str.split('; ')
    for (let i = 0; i < arr.length; i++) {
      let newArr = arr[i].split('=')
      if (newArr[0] === name) {
        return newArr[1]
      }
    }
  },
  del:function(name) {
    this.set(name, '', -1)
  },
 }
const fun={
  cookie:cookie, //cookie相关操作
}
if (typeof module !== "undefined" && module.exports) module.exports = fun;
if (typeof define === "function") define(function() { return fun; });
window.$fun=fun
})();
ES6 import/export
//cookie相关操作
export const cookie={
  set:function(name, value, day) {
    let oDate = new Date()
    oDate.setDate(oDate.getDate() + (day || 30))
    document.cookie = name + '=' + value + ';expires=' + oDate + "; path=/;"
  },
  get:function(name) {
    let str = document.cookie
    let arr = str.split('; ')
    for (let i = 0; i < arr.length; i++) {
      let newArr = arr[i].split('=')
      if (newArr[0] === name) {
        return newArr[1]
      }
    }
  },
  del:function(name) {
    this.set(name, '', -1)
  },
 };
ES6 export default
//cookie相关操作
const cookie={
  set:function(name, value, day) {
    let oDate = new Date()
    oDate.setDate(oDate.getDate() + (day || 30))
    document.cookie = name + '=' + value + ';expires=' + oDate + "; path=/;"
  },
  get:function(name) {
    let str = document.cookie
    let arr = str.split('; ')
    for (let i = 0; i < arr.length; i++) {
      let newArr = arr[i].split('=')
      if (newArr[0] === name) {
        return newArr[1]
      }
    }
  },
  del:function(name) {
    this.set(name, '', -1)
  },
 };
export default {
  cookie:cookie,
}

js本地存储函数封装基于localStorage本地存储