News for hackers. Stuff that matters.
Re:Fat arrow functions in Javascript by tieTYT on Friday September 20, 2013 @01:50PM (#6419329)
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?

Re:Fat arrow functions in Javascript by jontas on Friday September 20, 2013 @02:54PM (#6419772)
If 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));
        }
    }
Re:Fat arrow functions in Javascript by armandososa on Friday September 20, 2013 @01:56PM (#6419362)
Re:Fat arrow functions in Javascript by tieTYT on Friday September 20, 2013 @04:11PM (#6420192)
Great 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?
Re:Fat arrow functions in Javascript by gwwar on Friday September 20, 2013 @01:57PM (#6419368)
You can use Function.prototype.bind instead of creating a closure. Check out the examples here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...