Skip to content

Instantly share code, notes, and snippets.

@erichocean
Created May 17, 2012 18:36
Show Gist options
  • Save erichocean/2720807 to your computer and use it in GitHub Desktop.
Save erichocean/2720807 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title></title>
<script>
Object.defineProperty(Float32Array.prototype, 'width', {
get: function() {
return this[0];
},
set: function(val) {
this[0] = val;
},
enumerable: false,
configurable: false
});
var f = new Float32Array(1);
console.log('f[0] is', f[0], ', should be 0', f[0]===0? "" : "-- ERROR");
console.log('f.width is', f.width, ', should be 0', f.width===0? "" : "-- ERROR");
console.log('Doing: f.width = 2;')
f.width = 2;
console.log('f[0] is', f[0], ', should be 2', f[0]===2? "" : "-- ERROR");
console.log('f.width is', f.width, ', should be 2', f.width===2? "" : "-- ERROR");
console.log("Doing: Float32Array.prototype.__lookupSetter__('width').call(f, 2)")
Float32Array.prototype.__lookupSetter__('width').call(f, 2);
console.log('f[0] is', f[0], ', should be 2', f[0]===2? "" : "-- ERROR");
console.log('f.width is', f.width, ', should be 2', f.width===2? "" : "-- ERROR");
</script>
</head>
<body>
Check the JavaScript console output.
</body>
</html>
@erichocean
Copy link
Author

Output on Firefox:

f[0] is 0 , should be 0
f.width is 0 , should be 0
Doing: f.width = 2;
f[0] is 0 , should be 2 -- ERROR
f.width is 0 , should be 2 -- ERROR
Doing: Float32Array.prototype.__lookupSetter__('width').call(f, 2)
f[0] is 2 , should be 2
f.width is 2 , should be 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment