头图
Used well, the tool library and framework are indeed a great help, but we are afraid that we will get used to taking shortcuts and forget what our fundamental reliance is.

Preface

The rapid development of front-end technology has inevitably brought a sense of "fatigue" to practitioners, and we often lament that we can't learn. Thus, in order to "decompress" us, various tool libraries and frameworks were born.

For the company, through the introduction of tool libraries and frameworks, on the one hand, it restricts the code style and improves maintainability. The most important thing is that it can shorten the development cycle and achieve the finished product early.

For individuals, the various tool libraries and frameworks are simply not too cool to use, and there is no need to hum and gnaw on those native operating methods. It not only liberates the brain, but also frees up time for fishing. Consider the accuracy of the method... a multi-shot deal is simply a bargain!

The company is pursuing efficiency, and it is understandable to advocate the introduction of tool libraries and frameworks, but if we are personally addicted to this, then there is really a problem.

Of course, we can't deny the advantages of tool libraries and frameworks, but they can never be the cornerstones of our progress.

Used well, the tool library and framework are indeed a great help, but we are afraid that we will get used to taking shortcuts and forget what our fundamental reliance is.

I have a lot of emotion, but I really feel it. Today, a colleague in the test group asked me to write a script for them to remember the password. Because of the simple function, there is no need to introduce a tool library, but the native operation is used to implement it. As a result, I stumbled upon writing and had to go online halfway. research. It's such a simple realization, why is it so! ?

I have too many days to open my mouth, and I forgot how to cook it! I really want to know how many of us would be "starved to death" if there were no "food sources" one day?

Cookie operation

Regarding the related concepts of Cookie, if necessary, you can check here and here .

Set cookies

The cookie setting needs to include the following attributes:

  • key String type
  • value String type
  • expires optional, a timestamp conforming to the HTTP-date specification, and max-age (number, unit is second) can also be set. The setting is persistent cookie , and the default is session cookie .
  • path optional, String type
  • domain optional, String type
  • secure optional, String type

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;
}

Whether there is a cookie

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

Delete cookies

To delete a cookie, you only need to set its expiration time expires to the past time, or you can delete the 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;
}

Find cookies

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

Summarize

Wheels made by others may be easy to use, but in order to improve ourselves, we should also try to make wheels by ourselves. Even if it is rough, it is our own.

~

~ End of this article, thanks for reading!

~

Learn interesting knowledge, meet interesting friends, and create interesting souls!

Hello everyone, I is [ programming Samadhi 〗 author hermit king , my public number is " programming Samadhi " welcome attention, we hope the exhibitions!

Come, with expectations, I have Moxiang to greet you! You return, regardless of gains or losses, only with the lingering rhyme as a gift!

Pay equal attention to knowledge and skills, both internal and external skills, both theory and practice must be grasped, and both hands must be hard!


编程三昧
54 声望10 粉丝

学习有趣的知识,交识有趣的朋友,造就有趣的灵魂!