Created
July 28, 2012 18:08
-
-
Save dherman/3194222 to your computer and use it in GitHub Desktop.
Convert methods to unselfish functions
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
Function.prototype.unselfish = (function(Fp, Ap) { | |
var applyMethod = Fp.apply, | |
callMethod = Fp.call; | |
var apply = callMethod.bind(applyMethod), | |
call = callMethod.bind(callMethod); | |
var sliceMethod = Ap.slice; | |
return function unselfish() { | |
var f = this; | |
return function(self) { | |
return apply(f, self, call(sliceMethod, arguments, 1)); | |
}; | |
}; | |
})(Function.prototype, Array.prototype); | |
var hasOwn = Object.prototype.hasOwnProperty.unselfish(); | |
console.log(hasOwn({}, "toString")); // false | |
console.log(hasOwn({foo:1}, "foo")); // true |
I get it, thanks for clarification, good tip.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm writing in a "paranoid style", where as long as the library is initialized before any untrusted code, it will continue to work properly no matter how much someone trashes the standard libraries. Notice how your version calls
call.bind
, which could break if someone modifiesFunction.prototype.bind
orcall.__proto__
.For most applications this probably doesn't matter, but I was just playing around with just how "high-integrity" an implementation you can write.
Dave