function in object code example

Example 1: js functions inside of objects

var person = {
  name: "Fred",
  sayName: function() {
    console.log(this.name);
  }
};

person.sayName();

Example 2: function inside object javascript

var obj = {
  func: function(a, b) {
    return a * b;
  }
};

obj.func(3, 6); // 18

Example 3: 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)