// 1、创建cookie构造函数
function Cookie(){}
//set_cookie 把设置cookie方法绑定在cookie的原型对象上
Cookie.prototype.setCookie = function(key,value,time){
var date_ = new Date();
date_.setDate(date_.getDate()+time);
document.cookie=key+'='+value+';expires='+date_;
}
//获取cookie
Cookie.prototype.getCookie = function(key){
var arry = document.cookie.split('; ');
for(var i =0;i<arry.length;i++){
var n_arry = arry[i].split('=');
if(n_arry[0]==key){
return n_arry[1];
}
}
}
//删除cookie
Cookie.prototype.removeCookie = function(key){
this.setCookie(key,'',-1);
}
// 删除所有的cookie
Cookie.prototype.clearCookie = function(){
var arry = document.cookie.split('; ');
console.log(arry);
for(var i=0;i<arry.length;i++){
var n_arry = arry[i].split('=');
this.setCookie(n_arry[0],'',-1);
console.log(n_arry[0]);
}
}
使用原型对象封装cookie
最新推荐文章于 2024-08-27 13:38:21 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)