检索的document.cookie getter和setter [英] Retrieving document.cookie getter and setter
问题描述
我试图重写的document.cookie,因为我需要控制的cookie
创作,但目前看来,getOwnPropertyDescriptor浇铸在了document.cookie不检索它的getter和setter(尝试在Chrome和Firefox)。可能有人解释我这种行为?
https://jsfiddle.net/1363ktwp/7/
VAR的obj = {};\r
\r
//使用的document.cookie描述创建一个属性\r
Object.defineProperty(\r
OBJ,\r
oldCookie\r
Object.getOwnPropertyDescriptor(文件,曲奇)\r
);\r
\r
//设置Cookie成功地\r
的document.cookie =测试1 = OK;路径= /;过期= 365;;\r
的document.cookie =测试2 = OK;路径= /;过期= 365;;\r
\r
警报(的document.cookie);\r
\r
Object.defineProperty(文件,曲奇,{\r
得到:函数(){\r
返回obj.oldCookie;\r
},\r
设置:功能(饼干){\r
/ *\r
... preliminar操作\r
* /\r
\r
obj.oldCookie =饼干;\r
}\r
});\r
\r
// obj.oldCookie只是没有的getter / setter字符串\r
//所以下面的任务不会正常工作\r
的document.cookie =TEST3 = OK;路径= /;过期= 365;;\r
的document.cookie =TEST4 = OK;路径= /;过期= 365;;\r
\r
警报(的document.cookie);
\r
有人能解释我这种行为?
块引用>
的document.cookie
是的 主机对象的。主机对象往往不是真正的JavaScript对象(被称为的本机对象的),并既不要求也不保证有JavaScript对象的功能。我会深感震惊,事实上,如果许多或者甚至超过一个或两个浏览器中实现
的document.cookie
使用ES5属性getter / setter方法。也许对于一些较新的API(或者没有),但一个旧的,还有的将是一个很多关于克鲁夫特的。的(我也不得不考虑的安全后果很长一段时间......)的如果他们的没有的通过ES5 getter / setter方法实现它,如果他们做了它的不可配置的特性(它不会让我感到吃惊例如,使得安:我'T改变它)。
I'm trying to override the document.cookie since i need to control cookie creation, but seems that getOwnPropertyDescriptor casted on document.cookie doesn't retrieve its getter and setter ( tried on chrome and firefox ). Could someone explain me this behaviour?
https://jsfiddle.net/1363ktwp/7/
var obj={}; // creating a property using document.cookie descriptors Object.defineProperty( obj, "oldCookie", Object.getOwnPropertyDescriptor(document, "cookie") ); // setting cookies succesfully document.cookie="test1=ok;path=/;expires=365;"; document.cookie="test2=ok;path=/;expires=365;"; alert(document.cookie); Object.defineProperty(document, "cookie", { get: function () { return obj.oldCookie; }, set: function (cookie) { /* ...preliminar operations */ obj.oldCookie = cookie; } }); // obj.oldCookie is just a string without getter/setter // so assignments below doesn't works correctly document.cookie="test3=ok;path=/;expires=365;"; document.cookie="test4=ok;path=/;expires=365;"; alert(document.cookie);
解决方案Could someone explain me this behaviour?
document.cookie
is a property of a host object. Host objects are frequently not true JavaScript objects (called native objects) and are neither required nor guaranteed to have the features of JavaScript objects.I'd be truly shocked, in fact, if many or even more than one or two browsers implemented
document.cookie
using ES5 property getters/setters. Perhaps for some newer APIs (or perhaps not), but for one that old, there's going to be a lot of cruft about. (I'd also have to think a long time about the security ramifications...)If they did implement it via ES5 getters/setters, it wouldn't surprise me if they made it a non-configurable property (e.g., such that you couldn't change it).
这篇关于检索的document.cookie getter和setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!