Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 66e38e7

Browse files
bind
1 parent 1d55fc8 commit 66e38e7

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

‎04-functions.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,24 +169,34 @@ hello.apply(null, ['hey!']);
169169
* ```Function.prototype.call``` is just syntactic sugar over apply
170170
* call expects parameters as normal parameter list instead of an array
171171

172-
## partial application
172+
## function binding
173+
174+
* ```Function.prototype.bind``` has the same signature as ```.call```
175+
* creates a new function bound to the object and optional parameters passed in
176+
177+
## partial application / currying
173178

174179
* call a function with less than all of it's arguments
175180
* and return a function expecting the rest of the arguments
176181
* this transformation of function is called currying or schonfinkelizing
177182
* use when find yourself calling a function with same arguments
178183

179184
```js
185+
// ES5 way with bind
186+
var curried = add.bind(undefined, 10);
187+
curried(5);
188+
189+
// old way
180190
function curriedAdd(x,y) {
181-
if(y === undefined){
182-
return function(y){
191+
if(y === undefined){
192+
return function(y){
183193
return x+y;
184194
}
185195
}
186196
return x + y;
187197
}
188198

189-
//or with more general curry function
199+
//old way with general curry function
190200
function curry(fn){
191201
var slice = Array.prototype.slice; // needed because arguments is not a real Array?
192202
var storedArgs = slice.call(arguments, 1);
@@ -197,5 +207,5 @@ function curry(fn){
197207
};
198208
}
199209

200-
curry(add, 6)(7);
210+
curry(add, 10)(5);
201211
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /