As objects in JavaScript are used to represent real-world objects, the object methods are the functions that a real-world object can perform. Let's say a man is an object and he has many properties and some functions that he can perform. All those functions that an object can perform can be implemented using methods. The method is a fancy technical term used to define functions in the object. Like the object properties, we learned in the previous tutorial where we assign
name:value
pairs in the Object. Where
name
is a variable name and
value
is a data value. But in the case of methods, we define
value
as a function expression.
Syntax
declaration object_name = {method_name: function() {//function body} }
Example
<script> var man = { name: "Rahul", age: 20, //method is_adult: function() { return this.age>=18?true:false; } } </script>
Behind the code
In the above example
is_adult
is the method of
man
object. In the example, you can see that
is_adult
property name defines a function that returns
true
if the
this.age
value is greater than or equal to
18
else it returns false. In the example, we have used the
if..else
ternary operator
which is a shorthand to write the
if-else
statement. Inside the function/method body you can also notice that there I have used
this.age
statement to access the
age
property of the object man instead of directly using
age
. We will be covering
this
keyword further in this tutorial. Before that let's see how can we access an object method.
Access JS Object Methods
Methods are nothing, just the properties of an object which are defined as functions. And similar to the normal function calling we use the method name and parenthesis to call a method. But there is a slight difference, to call a method we use the dot operator between the object name and method calling. So the JS interpreter could understand we are calling the method that is defined inside a specific object. Syntax
object_name.method_name()
Example
<script> var man = { name: "Rahul", age: 20, //method is_adult: function() { return this.age>=18?true:false; } } console.log(man.is_adult()); // true </script>
JavaScript this keyword
this
is the most important keyword of JavaScript, in JavaScript, everything is an object, which means complete JavaScript revolves around the concept of objects. The objects we are learning right now are the building structure of all JavaScript concepts. And the
this
keyword is used when we are trying to access a property of the object inside the object method.
this
keyword tells the function to use the already defined
name:value
property of the object. Behind the scene
this
represent the object itself, in the above example, we have used
this.age
to access the
age
property of the
man
object inside the
is_aldult
method body. There this represents man so
this.age
inside the method was equivalent to
man.age
. So instead of
this.age
we could have used
man.age
inside the
is_adult
method body. Without using
this
keyword or object name itself we can not access the property object property inside the method.
Summary
- methods are the functions defined inside the object.
- to access a method we can use the object name, dot operator followed by the method name, and parenthesis.
-
this
keyword helps the method to access the property of the object.
People are also reading: