覆盖 .toString() 并作为对象返回

发布于 2024-11-29 02:25:30 字数 490 浏览 3 评论 0原文

在 JavaScript 中可能吗?

像这样的东西:

Response.Cookies = 
        function() {
            return document.cookie;
        };
Response.Cookies.toString = 
        function() {
           Cookies = {};
            this().replace(/([^=]+)=([^;]+);?/g,
                  function(foo, label, value) {
                           return Cookies[label] = value;    
                    });
             return Cookies;
};

alert(Response.Cookies); // "does not work"

is it possible in JavaScript?

Something like:

Response.Cookies = 
        function() {
            return document.cookie;
        };
Response.Cookies.toString = 
        function() {
           Cookies = {};
            this().replace(/([^=]+)=([^;]+);?/g,
                  function(foo, label, value) {
                           return Cookies[label] = value;    
                    });
             return Cookies;
};

alert(Response.Cookies); // "does not work"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抚你发端 2024-12-06 02:25:30

这就是我认为你想要的:

var Response = {};

Response.Cookie = 
        function() {
            var self = this;
            Cookies = {};
            this.Cookies = document.cookie;
            this.Cookies = this.Cookies.replace(/([^=]+)=([^;]+);?/g,
                  function(foo, label, value) {
                       Cookies[label] = value;    
                    });
            this.Cookies = Cookies;
        };
Response.Cookie();
for(var cookie in  Response.Cookies){
    alert(cookie + ' = ' +Response.Cookies[cookie])
}

小提琴:http://jsfiddle.net/ maniator/Yb8NK/


更新:

这是一个没有调用 Response.Cookie() 的版本:http://jsfiddle.net/maniator/Yb8NK/25/

var Response = {
    Cookies: null
}
Response.Cookie = (function() {
        var self = Response;
        Cookies = {};
        self.Cookies = document.cookie;
        self.Cookies = self.Cookies.replace(/([^=]+)=([^;]+);?/g, 
            function(foo, label, value) {
                 Cookies[label] = value;
            });
        self.Cookies = Cookies;
    })();
for (var cookie in Response.Cookies) {
    alert(cookie + ' = ' + Response.Cookies[cookie])
}

更新#2:

更好的版本:

var Response = {};
Response.Cookies = (function() {
    var cookies = {};
    var doc_cookies = document.cookie;
    doc_cookies = doc_cookies.replace(/([^=]+)=([^;]+);?/g, 
       function(foo, label, value) {
           cookies[label] = value;
       });
    return cookies;
})();
for (var cookie in Response.Cookies) {
    alert(cookie + ' = ' + Response.Cookies[cookie])
}

小提琴:http://jsfiddle.net/maniator/Yb8NK/29/

This is what i think you want:

var Response = {};

Response.Cookie = 
        function() {
            var self = this;
            Cookies = {};
            this.Cookies = document.cookie;
            this.Cookies = this.Cookies.replace(/([^=]+)=([^;]+);?/g,
                  function(foo, label, value) {
                       Cookies[label] = value;    
                    });
            this.Cookies = Cookies;
        };
Response.Cookie();
for(var cookie in  Response.Cookies){
    alert(cookie + ' = ' +Response.Cookies[cookie])
}

Fiddle: http://jsfiddle.net/maniator/Yb8NK/


UPDATE:

This is a version without ever calling the Response.Cookie(): http://jsfiddle.net/maniator/Yb8NK/25/

var Response = {
    Cookies: null
}
Response.Cookie = (function() {
        var self = Response;
        Cookies = {};
        self.Cookies = document.cookie;
        self.Cookies = self.Cookies.replace(/([^=]+)=([^;]+);?/g, 
            function(foo, label, value) {
                 Cookies[label] = value;
            });
        self.Cookies = Cookies;
    })();
for (var cookie in Response.Cookies) {
    alert(cookie + ' = ' + Response.Cookies[cookie])
}

UPDATE #2:

Even better version:

var Response = {};
Response.Cookies = (function() {
    var cookies = {};
    var doc_cookies = document.cookie;
    doc_cookies = doc_cookies.replace(/([^=]+)=([^;]+);?/g, 
       function(foo, label, value) {
           cookies[label] = value;
       });
    return cookies;
})();
for (var cookie in Response.Cookies) {
    alert(cookie + ' = ' + Response.Cookies[cookie])
}

Fiddle: http://jsfiddle.net/maniator/Yb8NK/29/

动次打次papapa 2024-12-06 02:25:30

不,toString() 需要返回一个字符串,否则对象到字符串的隐式转换(因为它由 alert() 执行)就会失败。你想达到什么目的?

No, toString() needs to return a string, otherwise the implicit conversion of objects to strings (as it is being performed by alert()) simply fails. What are you trying to achieve?

如此安好 2024-12-06 02:25:30

尝试调用 Response.Cookies,而不仅仅是引用它:

alert(Response.Cookies());

Try invoking Response.Cookies, rather than just referencing it:

alert(Response.Cookies());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文