@@ -831,8 +831,36 @@ function example(){
831
831
}
832
832
```
833
833
834
- * one typical example is borrowing ```Array`` methods for the ```arguments``` object
834
+ * one typical example is borrowing ```Array``` methods for the ```arguments``` object
835
835
836
836
### borrow and bind
837
837
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