来一波js最简化高效封装Cookie

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>封装cookie</title>
</head>
<body>
    
</body>
<script src="./cookie.js"></script>
<script>
    const {cookie}= cookies; //引入声明一下
    cookie.set('maomin', '22', '0.5');
    console.log(cookie.get("maomin"));
</script>
</html>



cookie.js

const cookies = {
    cookie: {
    // 设置cookie
        set: (name, value, day) => {
            const date = new Date();
            date.setDate(date.getDate() + day);
            document.cookie = name + "=" + value + ";expires=" + date;
        },
    // 获取cookie
        get: (key) => {
            var arr = document.cookie.split("; ");
            for (var i = 0; i < arr.length; i++) {
                var arr1 = arr[i].split("=");
                if (arr1[0] == key) {
                    return arr1[1]
                }
            }
            return ""
        },
     // 删除cookie
        remove: (name) => {
            cookie.set(name,'', -1)
        }
    }

}




作者:Vam的金豆之路

主要领域:前端开发

我的微信:maomin9761

微信公众号:前端历劫之路