JavaScript

In this tutorial, we're gonna talk about the most important type in JavaScript. It is Object.
Indeed, JavaScript treats everything as Object. Everything! :D
Let's get to know about the type.

Start Reading ;

An object in JavaScript

Whenever we lower down vibration to identify with the intellect which is the inferior dimension of the mind, everything seems to be separated and definable. Anything can be defined by some properties and abilities. For example: A tree can be considered as an object which has name, age, and ability to produce.

To reflect this dimension of the mind into programming, the first programmers of the world decided to allow modeling objects in code. This makes programming more friendly and gives people more possibilities to turn their ideas into code. Let’s have a look at the example below.

JS code:

Here we have an object stored in the variable theTree. An object in JavaScript is nothing but a bundle of variables and functions enclosed by a couple of curly braces {}. In the example above, the variables name and age are used to describe the tree’s properties; And the function produce is used to describe its ability to yield fruits.

Please take note that we don’t need to use the keywords var and function for bundled stuff of an object; Values are bound with the variables using colons : instead of equal signs; And things are separated by commas ,

Normally, when referring to object, the variables are called properties and the functions are called methods. But, it’s just the words matter, isn’t it? We’re gonna keep calling them variables and functions because they’re just that.

To access the bundled variables and functions, a dot . is used to chain the object’s reference and its bundled stuff. This is it! The dot which was mentioned in our first tutorial. We’ve used it many times with the statement console.log(). The console itself is an object; And the function log is actually bundled in the object.

Referring from the inside

In the section above, we’ve used the variable theTree to store the object’s reference; Then access the bundled stuff of the object from outside by calling the variable theTree. To use the object’s reference from the inside, the keyword this is used to refer to the object itself.

JS code:

Creating object using class

Yet, creating object in JavaScript can be done in a more elegance way. Let’s check the code snippet below.

JS code:

The keyword class is used to create an object named Thing which serves as a sketch. It does not have detail color and age; Instead, a constructor is used to bind givenColor and givenAge when an actual object is created later.

And then we’ve created 2 objects using the keyword new, the sketch’s name, and feeding detail color and age of each object. By creating objects this way, we don’t have to write much repetitive code like variable names and the function whisper which behave the same in the actual objects water and grass.

JavaScript also provides pre-defined classes and objects which give various conveniences. But, we’re gonna keep things simple now and save them for the next tutorials. Here is a small example which use a pre-defined class to get time information from system.

JS code:

Everything in JavaScript is Object

Not really. But, in fact, JavaScript treats almost everything as object. :D

A function is treated as an object. It’s a bundle.

JS code:

A number is also treated as an object. - Number Reference

JS code:

A string is treated as an object, too. - String Reference

JS code:

And we’ll meet more and more objects from now on. Our tutorial about object has finished. In the next tutorial, we’ll meet a built-in object of web browser which allows us to manipulate HTML document. Goodbye and see you next time in the realm of intellect. :D

In case you wonder the realm beyond, here is an invitation to the superior dimension of the mind, the dimension of knowing, where things are one, where words and thoughts reach their limitation…

Invitation:

Next: Document & Event ;