function in object js code example
Example 1: how to define function in object javascript
// Functions that are defined inside objects are called methods.
let example = {
calc: function (num1, num2) {
return num1 + num2;
},
};
// Calling the method
example.calc(1,2)
Example 2: function inside object javascript
var obj = {
func: function(a, b) {
return a * b;
}
};
obj.func(3, 6); // 18
Example 3: adding function to objects js
var myObj = {
myFunc: function(param){
//do stuff
}
}