浅析JS中document.cookie的用法2(悟)
//这篇随笔是在浅析JS中document.cookie的用法(悟)用函数基础中修改的;
//typeof 与instanceof的区别:
//typeof:用于判断对象的类型;对象,null,数组返回object;变量未定义,不存在返回undefined;
//instanceof:返回布尔值,用于判断对象是否属于一个特定的实例;
function setDate(day){ //设置过期时间
var date=null;
date=new Date()
if(typeof day=='number' && day>0 //判断是否是数字且大于0
){date.setDate(date.getDate()+day)}
else{
throw new Error('输入的必须是数字且必须大于0')} //否则抛出错误
return date;}
function setCookie(name,value,expires,path,domain,secure){ //创建cookie
var cookieName=encodeComponent(name)+'='+ encodeComponent(value)
if(expires instanceof Date){cookieName+=';expires'+expires} //失效时间判断
if(path){cookieName+=';path'+path} //路径判断
if(domain){cookieName+=';domain'+domain} //域名判断
if(secure){cookieName+=';secure'} //安全方面判断
}