开发者

Javascript - getOwnPropertyDescriptor & defineProperty on DOM prototype elements

开发者 https://www.devze.com 2023-03-25 03:35 出处:网络
I am trying to capture read/write operations on a开发者_开发知识库ny IMG tag\'s \"src\" attribute. For that purpose I was trying to use the getOwnPropertyDescriptor & defineProperty functions on t

I am trying to capture read/write operations on a开发者_开发知识库ny 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!


srcis a instance property, not a prototype property. Use something like the msdn example:

    var own = Object.getOwnPropertyDescriptor(HTMLImageElement.prototype, "setAttribute");

References

  • Approach of new Object methods in ES5
0

精彩评论

暂无评论...
验证码 换一张
取 消