Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sometimes there is a requirement to use _this #493

Closed
softwarespot opened this issue Aug 29, 2015 · 4 comments
Closed

Sometimes there is a requirement to use _this #493

softwarespot opened this issue Aug 29, 2015 · 4 comments

Comments

@softwarespot
Copy link

In the ES5 document it mentions using _this, but in ES6 it states () => {};. Surely there are occasions where _this is required? I use it sporadically of course, but I use it still.

@ljharb
Copy link
Collaborator

ljharb commented Aug 29, 2015

Can you think of one? I can't.

@softwarespot
Copy link
Author

This (pardon the pun) maybe...

App = {
    init: function () {
        // Init
        this.cacheDom(); // OK
    },
    cacheDom: function () {
        // Cache DOM
    },
    render: {
        page1: function () {
            // Need to re-cache DOM nodes
            // this.cacheDom(); // Doesn't work as this is referencing render.

            var _this = App.render;
            _this.cacheDom(); // This works
        },
        page2: function () {

        },
        page3: function () {

        }
    }
}

@ljharb
Copy link
Collaborator

ljharb commented Aug 29, 2015

In that scenario, App.render.cacheDom() works just fine. Using _this or self is for when you need to capture this for use in a closure - that doesn't apply to your scenario.

@softwarespot
Copy link
Author

Fair point. I can't really think of a concrete scenario right now (checked my code and seems I don't use it apart from what I mentioned above.) Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants