Yoav Weiss
Yoav Weiss

Reputation: 2270

Javascript - getOwnPropertyDescriptor & defineProperty on DOM prototype elements

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!

Upvotes: 2

Views: 1506

Answers (1)

Paul Sweatte
Paul Sweatte

Reputation: 24627

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

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

References

Upvotes: 2

Related Questions