获取并设置每个属性(对于 cookie)

问题描述 投票:0回答:1

我想做

cookie.c.something = "hello"
。这会将
document.cookie
设置为
"something=hello;"
。然后我就可以用
cookie.c.something
再次取回它。我已经做了get功能,但我不知道如何做set功能。 (当执行
cookie.c.something = "hello"
时,它会调用 getter)

我认为这可能可行,然后我意识到我无法在 set 函数中获取属性:

class cookie {
    static get c() {
        return this.object
    }

    /* What I want: */
    static set c(setter) {
        this.object["uses property 'hello'"] = setter
    }

    static get raw() {
        return document.cookie;
    }
    static get object(){
        let rem = {}
        const cookies = cookie.raw.split(';').map(cookie => cookie.trim())
        for (const cookieString of cookies) {
            const [cookieName, cookieValue] = cookieString.split('=')
            rem[`${decodeURIComponent(cookieName)}`] = decodeURIComponent(cookieValue)
        }
        return rem
    }
}

后来我发现:

let myObj = {
    id: 1,
    name: 'tomato',
};

for(let fieldName of Object.keys(myObj)) {
    console.log(`fieldName = ${fieldName}`);
    Object.defineProperty(myObj, fieldName + "_property", {
        set: function(value) {
            this[fieldName] = value;
        },
        get: function() {
            return this[fieldName];
        }
    });
}

myObj.id_property = 55; // setting id_property will set id
console.log(myObj.id); // so this will write 55

myObj.id = 123; // setting id
console.log(myObj.id_property); // this will write 123 because id_property returns id field

这几乎可以工作,但它只适用于我已经定义的一次。我希望它对所有人都有效。

javascript class getter-setter
1个回答
0
投票

工作代码:

class cookie {
    static get c() {
        return new Proxy(this.object, {
            set: function(target, property, value) {
                cookie.raw = `${encodeURIComponent(property)}=${encodeURIComponent(value)}`;
                target[property] = value;
                return true;
            }
        });
    }
    static get(name) {
        const cookies = cookie.raw.split(';').map(cookie => cookie.trim())
        for (const cookieString of cookies) {
            const [cookieName, cookieValue] = cookieString.split('=')
            if (cookieName === name) {
                return decodeURIComponent(cookieValue)
            }
        }
        return undefined
    }
    static set(name, value, { expires = false, domain = info.mainDomain, path = false, secure = false } = {}) {
        // console.log(name, value, expires, domain, path, secure)
        let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`
        if (expires instanceof Date) {
            cookieString += `; expires=${expires.toUTCString()}`
        }
        if (domain) {
            cookieString += `; domain=${domain}`
        }
        if (path) {
            cookieString += `; path=${path}`
        }
        if (secure) {
            cookieString += `; secure`
        }
        cookie.raw = cookieString
    }
    static delete(name, {domain = info.mainDomain, path = false, secure = false } = {}){
        this.set(name, "", {expires: new Date("Thu, 01 Jan 1970 00:00:01 GMT"), path: path, domain: domain, secure: secure})
    }
    static deleteAll({domain = info.mainDomain, path = false, secure = false } = {}){
        this.array.forEach((name)=>{
            console.log(name.cookieName)
            this.delete(name.cookieName, {expires: new Date("Thu, 01 Jan 1970 00:00:01 GMT"), path: path, domain: domain, secure: secure})
        })
    }
    static get raw() {
        return document.cookie;
    }
    static set raw(setter) {
        document.cookie = setter
    }
    static get array(){
        let rem = []
        const cookies = cookie.raw.split(';').map(cookie => cookie.trim())
        for (const cookieString of cookies) {
            const [cookieName, cookieValue] = cookieString.split('=')
            rem.push(
                {
                    cookieName: decodeURIComponent(cookieName),
                    cookieValue: decodeURIComponent(cookieValue)
                }
            )
        }
        return rem
    }
    static get object(){
        let rem = {}
        const cookies = cookie.raw.split(';').map(cookie => cookie.trim())
        for (const cookieString of cookies) {
            const [cookieName, cookieValue] = cookieString.split('=')
            rem[`${decodeURIComponent(cookieName)}`] = decodeURIComponent(cookieValue)
        }
        return rem
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.