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 19a6069

Browse files
bind
1 parent 7a0ca71 commit 19a6069

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

‎readme.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,36 @@ function example(){
831831
}
832832
```
833833
834-
* one typical example is borrowing ```Array`` methods for the ```arguments``` object
834+
* one typical example is borrowing ```Array``` methods for the ```arguments``` object
835835
836836
### borrow and bind
837837
838-
...
838+
* with call/apply ```this``` reference has to be passed in when called
839+
* sometimes it's better to locked/bound to specific object in advance
840+
* ```bind``` function binds a method to an object
841+
842+
```js
843+
//simple bind function
844+
function bind (object, method) {
845+
return function() {
846+
return method.apply(object, [].slice.call(arguments));
847+
}
848+
}
849+
```
850+
851+
### ```Function.prototype.bind```
852+
853+
* ES5 has bind builtin, it also accepts partial argument list
854+
855+
```js
856+
//basic implementation handling partial application
857+
Function.prototype.bind = Function.prototype.bind || function(thisArg) {
858+
var fn = this;
859+
var slice = Array.prototype.slice;
860+
var args = slice.call(arguments, 1);
861+
862+
return function () {
863+
return fn.call(thisArg, args.concat(slice.call(arguments)))
864+
}
865+
}
866+
````

0 commit comments

Comments
(0)

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