Javascript - getOwnPropertyDescriptor & DOM 原型元素上的 DefineProperty
我试图捕获任何 IMG 标签的“src”属性上的读/写操作。为此,我尝试使用 getOwnPropertyDescriptor & HTMLImageElement 对象上的 DefineProperty 函数(因为我想避免为每个 img 定义它们) 我对 getOwnPropertyDescriptor 的看法:
var proto = Object.getPrototypeOf(HTMLImageElement);
var own = Object.getOwnPropertyDescriptor(proto, "src");
// own is undefined in IE10/FF8/Chrome15
关于上面 proto 元素上的 DefineProperty,我看到 getter/setter 函数仅在 Chrome 中运行,但不是在我期望的时候运行,而且 getter 函数中的“this”是 DOM 的原型窗户。我的测试代码可以在 http://jsfiddle.net/yoav/tUekJ/
找到getOwnPropertyDescriptor 在这种情况下工作吗?我是否应该期望当 JS 访问“src”属性时触发 getter/setter 函数?
谢谢!
I am trying to capture read/write operations on any IMG tag's "src" attribute. For that purpose I was trying to use the getOwnPropertyDescriptor & defineProperty functions on the HTMLImageElement object (since I'd like to avoid defining them for each img)
What I saw regarding getOwnPropertyDescriptor:
var proto = Object.getPrototypeOf(HTMLImageElement);
var own = Object.getOwnPropertyDescriptor(proto, "src");
// own is undefined in IE10/FF8/Chrome15
Regarding defineProperty on the proto element above, I saw that the getter/setter functions only run in Chrome, but not when I would expect them to and that "this" inside the getter function is the prototype of the DOM window. My test code for this can be found at http://jsfiddle.net/yoav/tUekJ/
Should getOwnPropertyDescriptor work in this case? Should I expect the getter/setter functions to be triggered when JS accesses the "src" attribute?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
src
是一个实例属性,而不是原型属性。使用类似于 msdn 示例的内容:参考
src
is a instance property, not a prototype property. Use something like the msdn example:References