Created
March 25, 2010 12:30
-
-
Save matschaffer/343494 to your computer and use it in GitHub Desktop.
This file contains 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 sys = require('sys'), | |
buffer = '{"keys":[1,2,3]}'; | |
sys.p(JSON.parse(buffer)); | |
sys.puts("Before keys extension worked fine."); | |
function extend (obj, props) { | |
Object.getOwnPropertyNames(props).forEach(function(prop){ | |
var descriptor = Object.getOwnPropertyDescriptor(props, prop) | |
descriptor.enumerable = false | |
Object.defineProperty(obj, prop, descriptor) | |
}) | |
} | |
extend(Object.prototype, { | |
get keys() { | |
return Object.keys(this) | |
}, | |
set keys(val) { | |
// Original implimentation which breaks | |
// this.__defineGetter__('keys', function() { | |
// return val | |
// }); | |
var self = this, | |
setter = function(val) { | |
self.__defineGetter__('keys', function() { | |
return val; | |
}); | |
self.__defineSetter__('keys', setter); // Breaks w/o this line | |
}; | |
setter(val); | |
} | |
}); | |
sys.p(JSON.parse(buffer)); | |
sys.p(JSON.parse(buffer).keys); | |
sys.puts("If you see this, it worked. If not, it's busted."); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment