4 ms·
This article is written for someone who already understands the old issue that this is fixing (there's nothing wrong with writing it like that). But for someon
by tieTYT 13y ago
This article is written for someone who already understands the old issue that this is fixing (there's nothing wrong with writing it like that). But for someone like me that doesn't understand when you need to use this `bind()` stuff and the context your function is in (if I'm even saying that correctly), what can I read to get a better understanding of what's going on here?
In other words, what do I need to read before I can understand this article?
- armandososa 13y agothis is a good start http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/ http://yehudakatz.com/2011/08/11/understanding-javascript-fu...
- tieTYT 13y agoGreat link, that really clarified things for me (I think). The OP article uses this sample as something you can do with fat arrows. "Because the containing scope closes over the fat arrow function, you can do things like," let fib = (n) => { if (n <= 1) return 1; return fib(n - 1) + fib(n - 2}; } I don't understand what this is showing. Wouldn't this code work the same way without fat arrows because there's no `this` being used anywhere?
- gwwar 13y agoYou can use Function.prototype.bind instead of creating a closure. Check out the examples here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- jontas 13y agoIf you're writing OO javascript, you often want to use "this" to refer to your object, but various things in js and especially jquery can change the meaning/scoping of "this". So for example, if you have: var myObj = { foo: 'bar', baz: function() { console.log(this.foo); //would give you 'bar', as expected //but if you have some code that changes the meaning of this, for example: $('.button').click(function() { console.log(this.foo); //undefined, since now "this" refers to the scope of the event's callback function }); //but if you use bind: $('.button').click((function() { //now "this" is scoped to refer to the main object console.log(this.foo); //gives you 'bar' }).bind(this)); } }