Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Methods & this

JavaScript Methods

JavaScript methods are the actions or you can say a function that can performed any task.

Syntax

const objectName= { key1: value1, key2: function(){ //statements } };

Example

const identity = { firstName: 'John', lastName: 'Doe', age: 19, fullDetails: function() { return "I am " + this.firstName + " " + this.lastName + " and I am " + this.age + " years old"; } };

Here,

We have Successfully Declared the Object identity

Accessing Functions in Objects

Syntax

objectName.methodName();

Example:

const identity = { firstName: 'John', lastName: 'Doe', age: 19, fullDetails: function() { return "I am " + this.firstName + " " + this.lastName + " and I am " + this.age + " years old"; } }; identity.fullDetails(); // Output // I am John Doe and I am 19 Years Old

Here,

First , We have Declared an Object Identity and called its method fullDetails() which is Returning the Output.

This Keyword

You might be confused about this keyword, why it is used?

this keyword is used to refer to the object in which it it declared.

For Example: In Above Example we have declared an Object with name identity , then we have declared some properties and a method with name fullDetails

In the fullDetails Method, we have used this keyword to get the values of the key.

For Example : this.firstName is getting the value of Object identity with the Key firstName

this.lastName is getting the value of Object identity with the Key lastName

this.age is getting the value of Object identity with the Key age

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?