ES6 JavaScript— “this” keyword

ES6 JavaScript— “this” keyword

Every function created with the function keyword or with the method shorthand has its own “this”, and it generally binds to “who” called it. Arrow functions don’t bind “this” to anything, the “this” inside depends on the surrounding context

Try to summarize the usage of “this” with examples

  • Every function created with the function keyword or with the method shorthand has its own “this”, and it generally binds to “who” called it.
  • Arrow functions don’t bind “this” to anything, the “this” inside depends on the surrounding context

Example 1

“handler” declared by error function here, that means the “this” is nothing, just same as outside the handler, thus the output is Window object

const handler = () => {
 console.log(this);
};
btn1.addEventListener('click', handler);

Output

Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}

es6-in-depth self-learning javascript-tips es6-js self-taught javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Learning JavaScript: Data Types and Variables

To paraphrase the title of an old computer science textbook, “Algorithms + Data = Programs.” The first step in learning a programming language such as JavaScript is to learn what types of data the language can work with. The second step is to learn how to store that data in variables. In this article I’ll discuss the different types of data you can work with in a JavaScript program and how to create and use variables to store and manipulate that data.

Learning JavaScript: Working with Strings

Strings are the second most common data type used in JavaScript, and in many cases, since JavaScript is so widely used for web applications, it is the prominent data type.

Learning JavaScript: Statements, Arithmetic, and Math

In this article I will discuss how to perform arithmetic and more advanced mathematical operations in JavaScript. First, though, I need to discuss how statements are formed and used in JavaScript.

Learning JavaScript: Computing with Object Methods

JavaScript has a set of built-in methods you can use with your user-defined objects. In this article I’m going to discuss several of these methods and how you can use them in your JavaScript programs.

Learning JavaScript: Working with Objects — this, getters, and setters

JavaScript objects provide the reader with some special features that make object-based programmer more effective and more efficient. In this article I’m going to describe three of these features — the this keyword, getters, and setters. The this Keyword If there was ever a more confusing keyword in computer programming than thethiskeyword, I don’t know what it is.