We work with cookies as a javascript class

Hello! Today I want to consider a very popular question: how to work with cookies? Very different methods are offered everywhere, but which one is better? There is no definite answer to this question, but I want to offer a very convenient way, and if you are interested, welcome to the castes.



And so, to the point



We make the get_cookie, set_cookie and delete_cookie methods:



The code
var _cookie = { get_cookie:function(cookie_name){ var r=document.cookie.match('(^|;) ?'+cookie_name+'=([^;]*)(;|$)'); return r?unescape(r[2]):null }, delete_cookie:function(cookie_name){ var c=new Date(); c.setTime(c.getTime()-1); document.cookie=cookie_name+="=; expires="+c.toGMTString() }, set_cookie:function(name,value,exp_y,exp_m,exp_d,path,domain,secure){ var c=name+"="+escape(value); if(exp_y){ var expires=new Date(exp_y,exp_m,exp_d); c+="; expires="+expires.toGMTString() } if(path)c+="; path="+escape(path); if(domain)c+="; domain="+escape(domain); if(secure)c+="; secure"; document.cookie=c; } }
      
      





Well, we check these methods, and in principle, we can end this. But this is not what we are striving for. I'd like to do something like properties c #.



There is a very convenient proxy class in javascript for this.



In short, Proxy “wraps around” another object and can intercept (and, if desired, independently process) various actions with it, for example, reading / writing properties and others.



To work with proxy methods, we create two lambda functions:



The code
 var __methods = { get:(t,m)=>m in t?t[m]:_cookie.get_cookie(m), set:(t,m,v)=>v?_cookie.set_cookie(m,v):_cookie.delete_cookie(m) }
      
      







Now we create the Proxy class itself, the first parameter of which is the list of methods of the class, we will set it to _cookie, but if you do not need the methods of working with cookies, specify {}, the second parameter is the same __methods that we just created.



Great, now just write a simple line:



 var cookie = new Proxy(_cookie,__methods);
      
      





HOORAY! we created a class, by calling (get) the properties of which the __methods .get method will be called, and by assigning a value to it - __methods.set .



Usage example:



The code
 var name= prompt("  ","my_name"); var value= parseFloat(prompt("   ( float )","103")); cookie[name] = value; if(value<100) cookie[name]+=10; else cookie[name]-=10; //   alert(",  "+name+" = "+value+"   !"+"\n  :\n"+document.cookie);
      
      







Full code:
 var _cookie = { get_cookie:function(cookie_name){ var r=document.cookie.match('(^|;) ?'+cookie_name+'=([^;]*)(;|$)'); return r?unescape(r[2]):null }, delete_cookie:function(cookie_name){ var c=new Date(); c.setTime(c.getTime()-1); document.cookie=cookie_name+="=; expires="+c.toGMTString() }, set_cookie:function(name,value,exp_y,exp_m,exp_d,path,domain,secure){ var c=name+"="+escape(value); if(exp_y){ var expires=new Date(exp_y,exp_m,exp_d); c+="; expires="+expires.toGMTString() } if(path)c+="; path="+escape(path); if(domain)c+="; domain="+escape(domain); if(secure)c+="; secure"; document.cookie=c; } }; var __methods = { get:(t,m)=>m in t?t[m]:_cookie.get_cookie(m), set:(t,m,v)=>v?_cookie.set_cookie(m,v):_cookie.delete_cookie(m) }; var cookie = new Proxy(_cookie,__methods);
      
      







Compressed Code:
 var _c={g:function(d){var c=document.cookie.match("(^|;) ?"+d+"=([^;]*)(;|$)");return c?unescape(c[2]):null},d:function(d){var c=new Date;c.setTime(c.getTime()-1),document.cookie=d+="=; expires="+c.toGMTString()},s:function(k,r,i,a,n,q,o,c){var e=k+"="+escape(r);i&&(e+="; expires="+new Date(i,a,n).toGMTString());q&&(e+="; path="+escape(q)),o&&(e+=";domain="+escape(o)),c&&(e+="; secure"),document.cookie=e}},cookie=new Proxy({get_cookie:_c.g,delete_cookie:_c.d,det_cookie:_c.s},{get:(d,c)=>c in d?d[c]:_c.g(c),set:(e,c,f)=>f?_c.s(c,f):_c.d(c)});
      
      







Please love and favor in the comments)



All Articles