This is the 28th day of my participation in the August Challenge

When used well, libraries and frameworks can be a great help, but the fear is that we get so used to taking shortcuts that we forget what we really rely on.

preface

The rapid development of front-end technology has inevitably brought a sense of “fatigue” to practitioners, and we often sigh that learning is not moving. As a result, tool libraries and frameworks have been created to relieve our stress.

For the company, the introduction of tool libraries and frameworks can, on the one hand, constrain the code style, improve the maintainability, and most importantly, shorten the development cycle and produce products earlier.

For individuals, all kinds of tool libraries and frameworks are simply not too cool to use, no longer need to chew on those original operation methods, not only liberate the brain, and more time to fish, but also do not have to consider the accuracy of the method…… It’s a great deal to buy more than one arrow!

Companies are cost-conscious and there’s nothing wrong with introducing libraries and frameworks, but if we’re addicted to them as individuals, there’s a real problem.

Of course, we can’t deny the advantages of tool libraries and frameworks, but it’s never going to be tool libraries and frameworks that will be the cornerstone of our progress.

When used well, libraries and frameworks can be a great help, but the fear is that we get so used to taking shortcuts that we forget what we really rely on.

It’s a little emotional, but it’s really emotional. Today, a colleague of the test team asked me to write a script to remember passwords for them. Considering the simple function and no need to introduce a tool library, I used the native operation to achieve it. As a result, I stumbled in writing and had to search information online during the process. It’s such a simple implementation. ?

I forget how to cook when I have too many meals to eat! I really want to know, if one day there is no “food source”, how many of us will be “starved to death”?

The operation of the Cookie

For more information about cookies, see here and here.

Set the Cookie

Cookie Settings need to include the following properties:

  • Key type String
  • The value type String
  • This parameter is optional. Expires indicates a timestamp that conforms to the HTTP-date specification. You can also set max-age (number, in seconds). The persistent Cookie is set, and the session Cookie is set by default.
  • Path Is an optional String
  • Domain Optional. The value is a String
  • Secure This parameter is optional. The value is a String

A simple way to set cookies:

function setCookieItem(sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    if(! sKey ||/ ^ (? :expires|max\-age|path|domain|secure)$/i.test(sKey)) {
        return false;
    }
    var sExpires = "";
    if (vEnd) {
        switch (vEnd.constructor) {
            case Number:
                sExpires = vEnd === Infinity 
                    ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" 
                	: "; max-age=" + vEnd;
                break;
            case String:
                sExpires = "; expires=" + vEnd;
                break;
            case Date:
                sExpires = "; expires=" + vEnd.toUTCString();
                break; }}document.cookie = encodeURIComponent(sKey) 
        + "=" + encodeURIComponent(sValue) 
        + sExpires 
        + (sDomain ? "; domain=" + sDomain : "") 
        + (sPath ? "; path=" + sPath : "") 
        + (bSecure ? "; secure" : "");
    return true;
}
Copy the code

Whether cookies exist

function isCookieItemExisted(sKey) {
    return new RegExp("(? : ^ |; \\s*)" + encodeURIComponent(sKey).replace(/[-.+*]/g."\ \ $&") + "\\s*\\=").test(document.cookie);
}
Copy the code

Delete the Cookie

To delete a Cookie, set expires to the past time. You can also delete a Cookie by setting max-age to 0 or -1:

function removeCookieItem(sKey, sPath, sDomain) {
    if(! sKey || ! isCookieItemExisted(sKey)) {return false;
    }
    document.cookie = encodeURIComponent(sKey) 
        + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" 
        + (sDomain ? "; domain=" + sDomain : "") 
        + (sPath ? "; path=" + sPath : "");
    return true;
}
Copy the code

Find the Cookie

function getCookieByKey(sKey) {
    return decodeURIComponent(document.cookie.replace(new RegExp("(? : (? : ^ |. *) \\s*" + encodeURIComponent(sKey).replace(/[-.+*]/g."\ \ $&") + "\\s*\\=\\s*([^;] *). * $) | ^. * $"), "$1")) || null;
  },
Copy the code

conclusion

Other people’s wheels may work, but in order to improve ourselves, we had better try to build our own wheels, even if they are rough, but they are our own.

~

Thanks for reading!

~

Learn interesting knowledge, meet interesting friends, shape interesting soul!

Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!

You come, with expectations, I have ink to welcome! You return, no matter gain or loss, only to yu Yun give each other!

Knowledge and skills should be paid equal attention to, internal force and external power should be repaired simultaneously, theory and practice should grasp both hands, both hands should be hard!