Thursday, November 20, 2025
HomeLanguagesJavascriptHow to create a function that invokes the method at a given...

How to create a function that invokes the method at a given key of an object in JavaScript ?

In JavaScript objects store variables in the key-value pair where the key is the property of the object and the value maybe the value of the property, or it may be any method that invokes when it is called.

In this article, we will learn to invoke a method that is provided to key.

Approach:

Generally, an external function can be defined to do the same, but we need to access every key of the object instead we can directly define a function to a key in the object and access the function only.

Syntax:

var objectName=
{
key1: value or method(),
key2: value or method(),
key3: value or method(),
...
}

Code: The following code snippet demonstrates to access Object.keys as myProfile.myName and myProfile.skills gets the name and skills for the object myProfile.

Javascript




<script>
  var myProfile={
       myName: 'Lokesh',
       skills: 'Web Development'    
  }
  
  let message=`My name is ${myProfile.myName} 
               and my skills are ${myProfile.skills}`;
  
  console.log(message);
</script>


Output:

My name is Lokesh and my skills are Web Development.

Declaring a function to a key of an object: The following code snippet demonstrates a function printProfile() for the object myProfile.

Javascript




<script> 
  
    var myProfile={
     myName: 'Lokesh',
     skills: 'Web Development',
      printProfile : function()
      {
       let message=`My name is ${this.myName} 
                    and my skills are ${this.skills}.`;
       return message;
      }
    }
  
    console.log(myProfile.printProfile());
  
</script>


Output:

My name is Lokesh and my skills are Web Development.

Note: The this keyword is used to access the same object key’s value and can be accessed using this.keyname.

Calculator by invoking the methods of keys in an object: The following code snippet demonstrates many functions’ adder(), subtractor(), multiplier(), divider() for the object calculator.

Javascript




<script>  
  var calculator={
      adder: function(num1,num2){
         let num=num1+num2;
         return num;
      },
      subtractor: function(num1,num2){
          let num=num1-num2;
          return num;
       },
      multiplier: function(num1,num2){
          let num=num1*num2;
          return num;
       },
       divider: function(num1,num2){
          let num=num1/num2;
          return num;
       }
  }
  console.log(calculator.adder(5,5));
  console.log(calculator.subtractor(5,5));
  console.log(calculator.multiplier(5,5));
  console.log(calculator.divider(5,5));
 </script>


Output:

 10
 0
 25
 1
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6780 POSTS0 COMMENTS
Nicole Veronica
11927 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11995 POSTS0 COMMENTS
Shaida Kate Naidoo
6906 POSTS0 COMMENTS
Ted Musemwa
7164 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS