3 ms·
Advanced JavaScript: Compose function
- apphacker 16y agoThis is simply bind which will be in future versions of JavaScript. http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/ http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more... It has been available in dojo as dojo.hitch, goog.bind, $.hitch in a jQuery plugin.
- ionfish 16y agoNo, it's function composition as seen in e.g. pure functional programming languages like Haskell: (.) :: (b -> c) -> (a -> b) -> a -> c Except that of course in an impure language like JavaScript if you compose some function f with another function g, either or both functions could have side effects (via I/O, mutation etc.) that break referential transparency. After a quick glance at the Prototype code, the bind function isn't merely changing which object the name 'this' is bound to, it also allows a kind of currying. However, this still isn't function composition.
- apphacker 16y agoLook at the source of what dojo.hitch does or google.bind. It is the exact same thing. Whoever down-voted me obviously doesn't know JavaScript. Voting on comments is such a dumb idea. Popularity does not equal right. Right is simply right.
- heseltine 16y ago@apphacker - are you sure? I thought bind assigned the value of 'this' in a function.
- pg 16y agoWe have nested comments here. You don't have to use @; you can reply directly.
- apphacker 16y agoWhat is 'call' doing in that function? Check out dojo.hitch: http://www.google.com/codesearch/p?hl=en#1oUPVh-C1Wg/trunk/eval/gx/zendframework/externals/dojo/dojo/_base/lang.js&q=dojo.hitch&sa=N&cd=1&ct=rc http://www.google.com/codesearch/p?hl=en#1oUPVh-C1Wg/trunk/e...