Created
February 1, 2015 00:27
-
-
Save DigiTec/bf9a51815849ecdd3693 to your computer and use it in GitHub Desktop.
ES 5 Configurable Properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://jsfiddle.net/s4ex1dtj/ | |
| "use strict"; | |
| var myImage = new Image(); | |
| Object.defineProperty(myImage, "src", { | |
| get: function () { return 'foo'; }, | |
| set: function (val) { alert(val); } }); | |
| console.log(myImage.src); // Logs value 'foo' | |
| myImage.src = "new"; // alerts value 'new' | |
| // newImage does not have a redefined setter since our previous property | |
| // definition was only on the myImage instance | |
| var newImage = new Image(); | |
| console.log(newImage.src); // Logs browser src getter value | |
| // Fix newImage and all existing and future images using the prototype | |
| Object.defineProperty(HTMLImageElement.prototype, "src", { | |
| get: function () { return 'foo'; }, | |
| set: function (val) { alert(val); } }); | |
| console.log(newImage.src); // Logs value 'foo' | |
| newImage.src = "yay!"; // alerts value 'yay!' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var myImage = new Image(); | |
| console.log(myImage.src); // Log the browsers natural src getter | |
| myImage.src = "http://wwww.foo.com/myImage.png"; // Use the browsers natural src setter | |
| console.log(myImage.src); // Log the browsers natural src getter after setting |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://jsfiddle.net/2aetdjj9/ | |
| // Get the property descriptor for the original browser src property | |
| var propertyDescriptorSrc = Object.getOwnPropertyDescriptor(HTMLImageElement.prototype, "src"); | |
| alert(propertyDescriptorSrc.get); // alerts function set() { [native code] } | |
| // Wrap the property descriptor and add some logging | |
| Object.defineProperty(HTMLImageElement.prototype, "src", { | |
| get: function get_src() { | |
| var val = propertyDescriptorSrc.get.call(this); | |
| alert('getting src: ' + val); | |
| return val; | |
| }, | |
| set: function (val) { | |
| alert('setting src to: ' + val); | |
| propertyDescriptorSrc.set.call(this, val); | |
| } | |
| }); | |
| var myImage = new Image(); | |
| myImage.src; // alert's the source in our pass through getter | |
| // Now lets verify our pass through setter | |
| myImage.src = 'https://camo.githubusercontent.com/eb464a60a4a47f8b600aa71bfbc6aff3fe5c5392/68747470733a2f2f7261772e6769746875622e636f6d2f766f6f646f6f74696b69676f642f6c6f676f2e6a732f6d61737465722f6a732e706e67'; | |
| // Verify the native src was actually set (note: 'target' is defined in the fiddle) | |
| document.getElementById('target').appendChild(myImage); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment