Javascript - getOwnPropertyDescriptor & defineProperty on DOM prototype elements
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!
src
is 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
精彩评论