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

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.

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. However, the concept behind this is not really that hard to understand. The keyword this refers to the current object that is in reference where the code using it is executed.

Examples are often better than explanations, so here is an example. First, let’s write an object constructor function and a short program to test it:

function Person(n, a) {
  this.name = n;
  this.age = a;
}
let me = new Person("Mike", 63);
print(me.name + ", " + me.age);

The output from this program is:

Mike, 63

The this keyword is used to reference the current object properties in memory when the code is running. You can translate it as “assign the value in n to the name property of this particular object.”

Another great use of this is to disambiguate code where the property name is the same as the parameter name, as in this rewrite of the constructor function:

function Person(name, age) {
  this.name = name;
  this.age = age;
}

You can’t leave the this keyword out of a constructor definition but it still helps make the code clearer to the reader.

The this keyword plays an important role in working with objects in JavaScript and I hope I’ve made its use clearer to you.

javascript-development learning-javascript learn-to-code javascript learn-to-program

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: Development Environments for JavaScript Programming

One of the nice things about learning JavaScript these days is that there is a plethora of choices for writing and running JavaScript code. In this article, I’m going to describe a few of these environments and show you the environment I’ll be using in this series of articles.

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 to Code: How to Boost Up the Process?

Learning to Code: How to Boost Up the Process? I also often recommend different online and offline resources to my students to make their learning process easier, more effective, and faster. And in this post, I will share a few tips with you.

Learning JavaScript: for Loops

In this article I’m going to cover the different forms of the for loop that are available in JavaScript. There is the general for loop, the for..in loop, and the for..of loop. I will describe how each loop works and when is the right time to use each loop type. I am leaving out the Array.forEach loop as it is specialized for arrays and requires some knowledge of functions I haven’t covered yet.

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.