From the course: JavaScript: Best Practices for Functions and Classes

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use this only in classes or constructors

Use this only in classes or constructors - JavaScript Tutorial

From the course: JavaScript: Best Practices for Functions and Classes

Start my 1-month free trial

Use this only in classes or constructors

- Java Script's "this" keyword is an indispensable shortcut in some situations, but if it's misused, it can result in confusing bugs or code that's needlessly complicated. Although Java Script technically supports using "this" in most any statement, you'll get the most affective use out of it by limiting your use to classes or constructors. Within a class or constructor, the context of "this" is straightforward. In addition, the syntax of both classes and constructors incorporate use of "this" to make code for basic operations easier to write. ESLint includes a rule that enables you to flag uses of "this" outside of classes or constructors. The no-invalid-this rule. The rule includes a few other cart routes for situations where "this" is appropriate, but in general, classes are the main situation that makes sense, and this rule helps enforce that. In my app.js file for this video, I've added code that calculates tax…

Contents