Introduction and Basics of JavaScript | Part 6

Before continuing with basic JavaScript, if you haven’t read part 5, please read it first at Introduction and Basics of JavaScript | Part 5. Without further ado, let’s get straight to the basics of JavaScript.

Object Method

In the object data type, we have already discussed the properties of the object. Because actually, a function is also a data type, so a function can be added as a property in an object.

The way of making it is the same as the function as a value. Functions in objects are sometimes referred to as Object Method

Keyword this

this keyword is a reference to whose object. Each location for this keyword can have different owner references.

In Object Method, this is a reference to the object that owns the function
In the global scope, this is a reference to the global object (in browsers it is usually Window)

console.info(this) // window

In a function, this is a reference to a global object (in browsers it is usually a Window)

In functions with strict mode (discussed later), this is undefined. In events, this is a reference to the element that received the event (discussed in the Document Object Model material)

Arrow Function di Object

Previously we have discussed Arrow Function. We can also use Arrow Function as an Object Method

However, please note, that Arrow Function cannot be used to access arguments object, function generator, this keyword, and super keyword (discussed in Object-Oriented Programming JavaScript material).

So make sure to use the Arrow Function only when we don’t need the features above

Getter and Setter

Getter and Setter are the ability to create different functions to retrieve data (Getter) and change data (Setter) on a property in the Object.

By using Getter and Setter, we can do anything in a function before a property is accessed or changed, for example adding validation and others.

What’s Wrong with var?

Previously it was explained that the use of var was no longer recommended, and was replaced with let

The question is why? Var is not recommended anymore because it doesn’t have block scope, meaning it doesn’t bind to the local scope we discussed earlier.

Due to this problem, var can be unpredictable. We will make an example of using var and let

Destructuring

Destructuring is a feature that can be used to disassemble values in arrays or objects into variables

This feature makes it very easy for us when we want to retrieve data from an array or object without having to take data one by one

Retrieving Data Array

But if we use destructuring it becomes

Retrieving Data in Object

But if we use destructuring it becomes

Destructuring Nested Object

Destructuring Function Parameter

Destructuring can not only be done in variables but can also be done in the function parameter.

This makes it easy for us when we want to retrieve nested data in arrays or objects in functions.

Default Value

The most interesting thing in destructuring is, that we can add a default value. So, for example, if we do destructure an array, it turns out that there is no data, then we can add a default value

Likewise for objects, if it turns out that the property doesn’t exist, then we can add a default value

Using Other Variable Names

When destructuring an array, we can easily create variable names as we like. But when doing destructuring, we have to make the variable name the same as the property name. We can also use other variable names when destructuring objects if we want

Strict Mode

When we run JavaScript program code, by default our program code runs in non-strict mode, or the term sloppy mode

In ECMAScript 5, strict mode was introduced, where when strict mode is run, it will change several ways of working in JavaScript, such as:

  • Changed some JavaScript errors from silent errors to throw errors (visible)
  • Fixed some JavaScript engine errors for optimization
  • Reject some code commands that will be used in ECMAScript in the future ( developer.mozilla.org)

How to Turn on Strict Mode

To enable strict mode, we can add `use strict` to the first line of our JavaScript file. Or it can be added at the beginning of our function

Debugger

Debugger is used to perform the debugging process. Debugging is the process of looking for bugs (problems) that usually occur in our program code

With the debugger, we can stop the program code at the position we want (breakpoint), then see all the contents of the variables that exist when the program code is stopped.

That’s a summary of what I learned about basic JavaScript that I wrote from parts 1 to 6, if there are mistakes and shortcomings I apologize

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store