How to create a function that invokes the method at a given key of an object in JavaScript ?
Last Updated :
21 May, 2021
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
Similar Reads
How to create a function that invokes function with partials prepended arguments in JavaScript ? In this article, we will see how to create a function that invokes functions with partials prepended to the arguments it receives in JavaScript. Before understanding the problem statement and approaching the solution for the same, let's, first of all, know what a function (also called a method) is a
5 min read
How to create a function that invokes function with partials appended to the arguments in JavaScript ? In this article, we will learn to create a function that invokes a function with partials appended to the arguments it receives in JavaScript. Approach: We want to implement a function that invokes another function and provides the arguments it received. We can get the result by using (...) the spre
3 min read
How to create a function that invokes the provided function with its arguments transformed in JavaScript ? In programming, functions are used to reduce the effort of writing the same instance of code repeatedly. In this article let us see how we can create a function that invokes the provided function with its arguments transformed in JavaScript. In this article, the function transformer invokes the func
2 min read
How to create a function that invokes each provided function with the arguments it receives using JavaScript ? In this article, we will see how to create a function that invokes each provided function with the arguments it receives using JavaScript. It helps in reducing the effort of creating the same instance of code again and again. In this article let us see how to create a function that invokes each prov
3 min read
How to create an object from the given key-value pairs using JavaScript ? In this article, we will learn to create an object from the given key-value pairs using JavaScript.Key-value pair: Unique identifier (key) associated with its corresponding value; fundamental for data organization and retrieval in programming. here we have some common approaches.We will explore the
5 min read
How to get the first key name of a JavaScript object ? In JavaScript, accessing the first key name of an object involves identifying the initial property defined within that object. This concept is useful when you need to interact with or manipulate object properties in a specific order, particularly when the sequence of properties is relevant.Here we h
2 min read