Understanding JavaScript Object
Objects in JavaScript, just as in many other programming languages, can be compared to objects in real life. The concept of objects in JavaScript can be understood with real life, tangible objects.
In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc.
WHAT IS AN OBJECT ?
An object is a collection of properties, and a property is an association between a name (or key) and a value. A propertyβs value can be a function, in which case the property is known as a method.
Hence, we can say JavaScript object is a collection of named values
From the above illustration, we declared a var person (var means variable, it acts as a temporary location for data storage and changes along with program execution) . The var person is now used to store information about a certain person.
Object Properties
The named values, in JavaScript objects, are called properties.
Property β β β β β β Value
firstName β β β β β Ogunremi
lastName β β β β βMarian
age β β β β β β β β25
maritalStatus β β β βMarried
Creating a JavaScript Object
With JavaScript, you can define and create your own objects.
There are different ways to create new objects:
- Define and create a single object, using an object literal.
- Define and create a single object, with the keyword
new
.
Using an Object Literal
This is the easiest way to create a JavaScript Object.
Using an object literal, you both define and create an object in one statement.
An object literal is a list of name:value pairs (like age:25) inside curly braces {}.
Using the JavaScript Keyword new
The following example also creates a new JavaScript object with four properties:
JavaScript Object Methods
From the code snippet above, var person is declared and will be used to stored the property and value of our object. After defining the firstName, lastName, id , we decided to also define a method or function which when called it returns a concatenated value of the firstName and lastName.
NOTE : this is a keyword which is used to refer to the current window object. So in our case this.firstName = person.firstName.
JavaScript Math Object
The JavaScript Math object allows you to perform mathematical tasks on numbers.
Math.round()
Math.round(x)
returns the value of x rounded to its nearest integer:
Math.round(4.7); // returns 5
Math.round(4.4); // returns 4
Math.pow()
Math.pow(x, y)
returns the value of x to the power of y:
Math.pow(8, 2); // returns 64
Math.sqrt()
Math.sqrt(x)
returns the square root of x:
Math.sqrt(64); // returns 8
Math.min() and Math.max()
Math.min()
and Math.max()
can be used to find the lowest or highest value in a list of arguments:
Math.min(0, 150, 30, 20, -8, -200); // returns -200Math.max(0, 150, 30, 20, -8, -200); // returns 150
Math.random()
Math.random()
returns a random number between 0 (inclusive), and 1 (exclusive):
Math.random(); // returns a random number
CONCLUSION
A quick introduction to javascript objects. It focuses on how to use objects, properties, functions or methods, and how to create your own objects.
Happy coding πππππππ