Created
February 11, 2017 04:44
-
-
Save ahmed-anas/2c5d3fe08ff27ffb276d8720677ad3f5 to your computer and use it in GitHub Desktop.
a basic implementation of bind without various properties
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
| let myObj = { | |
| str: 'Original Scope', | |
| foo: function() { | |
| console.log(`my scope is ${this.str}.`); | |
| } | |
| } | |
| function myBind(func, scope){ | |
| for(let prop in scope){ | |
| this[prop] = scope[prop]; | |
| } | |
| this.funcToCall = func; | |
| return this.funcToCall; | |
| } | |
| myObj.foo(); | |
| var newFoo = myBind(myObj.foo, {str: 'Overwridden Scope'}); | |
| newFoo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment