Object-Oriented JavaScript — Polymorphism and Primitive Values

John Au-Yeung - Jan 24 '21 - - Dev Community

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

JavaScript is partly an object-oriented language.

To learn JavaScript, we got to learn the object-oriented parts of JavaScript.

In this article, we’ll go through the object-oriented parts of JavaScript.

We also look at the basic building block of objects, which are primitive values.

Polymorphism

Polymorphism means that if we substitute one subclass with another, that the program should still work the same way if the code uses the parent’s properties or methods.

If they both inherit from the same parent class, then they should have the same methods, so they should work the same way.

If the Person class has the talk method and we created 2 subclasses that inherit from them.

Then we can use any of those subclass’ talk method.

Primitive Data Types

The basic building blocks of object-oriented JavaScript are primitive data types.

In JavaScript, primitive data types are strings, number, booleans, null , undefined and bigint.

They’re all recorded as values and have no methods of their own unless they’re wrapped in wrapper objects.

Variables

Variables are used to store data.

They’re placeholders for concrete values.

It’s more convenient to use variables than the values to wor with the actual data.

This is because they can be used multiple times.

Values can only be used once.

Using variables requires 2 steps.

We’ve to declare it, and then we’ve to assign it a value.

To declare a variable, we can write:

let a;
Enter fullscreen mode Exit fullscreen mode

A variable can be a combination of letters, numbers, and underscore characters.

But we can’t start with a number.

Then to initialize it, we assign a value to it.

For instance, we can write:

let a = 1;
Enter fullscreen mode Exit fullscreen mode

We can do both in one step.

Variables declared with const requires a value to be assigned when it’s declared.

So we write something like:

const a = 1;
Enter fullscreen mode Exit fullscreen mode

We can declare multiple variables in one statement, like:

let a = 1,  
  b = 2,  
  c = 3;
Enter fullscreen mode Exit fullscreen mode

We can also use $ in variables.

Variable names can start with $ and it can also be in other positions.

$ is often used as function or property names.

Variables are Case Sensitive

JavaScript variables are case sensitive.

So if they’re different cases, then they’re considered different.

Operators

Operators can take one or values or variables, do an operation, and return a value.

JavaScript has various operators like arithmetic operators, bitwise operators, etc.

+ adds 2 numbers or concatenate strings.

- does subtraction.

* does multiplication.

/ does division.

% returns the remainder of one number divided by another.

++ increments the number by 1.

-- decrements the number by 1.

For instance, we can add 2 numbers by writing:

const sum = 1 + 2;
Enter fullscreen mode Exit fullscreen mode

There are also some compound operators.

They make our code more compact.

They do the arithmetic and the assignment at the same time.

+= adds a number to an existing number.

-= subtracts a number from an existing number.

*= multiplies a number with an existing number.

/= divides a number from an existing number.

%= returns the remainder if we divide the left number by the right number.

For instance, we add a number to a variable by writing:

let x = 1;  
x += 2;
Enter fullscreen mode Exit fullscreen mode

We created a variable x and set it to 1.

Then we add 2 to x with += .

So at the end x is 3.

Conclusion

Polymorphism is where we can substitute a subclass with another and we can still use the parent’s methods.

Variables hold values and we can do various operations with them.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player