Last updated: Mar 4, 2024
Reading timeยท2 min

You can call a function inside an object by declaring the function as a
property on the object and invoking it, e.g. obj.sum(2, 2).
An object's property can point to a function, just like it can point to a string, number or other values.
const obj = { sum(a, b) { return a + b; }, }; console.log(obj.sum(10, 10)); // ๐๏ธ 20 console.log(obj.sum(10, 20)); // ๐๏ธ 30
We declared a sum property on an object. The property points to a function.
const obj = { sum(a, b) { return a + b; }, }; console.log(typeof obj.sum); // ๐๏ธ "function"
. or bracket [] notation to call the function.const obj = { sum(a, b) { return a + b; }, }; // ๐๏ธ using dot notation console.log(obj.sum(10, 10)); // ๐๏ธ 20 // ๐๏ธ using bracket notation console.log(obj['sum'](10, 10)); // ๐๏ธ 20
We used a shorthand property to define the function in the object.
When reading older code, you might see the following more verbose and outdated approach.
// ๐๏ธ older syntax const obj = { sum: function (a, b) { return a + b; }, }; console.log(obj.sum(10, 10)); // ๐๏ธ 20 console.log(obj.sum(10, 20)); // ๐๏ธ 30
The first approach is more concise and easier to read.
this keyword to access the object's propertiesYou can use the this keyword to access the object's properties inside of the
function.
const obj = { num: 100, sum(a, b) { return a + b + this.num; }, }; console.log(obj.sum(1, 1)); // ๐๏ธ 102
In this particular context, the this keyword refers to the object.
You could also add a function to the object after it has been declared.
const obj = { num: 100, }; obj.sum = function (a, b) { return a + b + this.num; }; console.log(obj.sum(10, 10)); // ๐๏ธ 120
Notice that we used the function keyword to define the function.
Had we used an arrow function, the value of the this keyword would not be
correctly bound and would point to the enclosing scope (not the object).
const obj = { num: 100, }; obj.sum = (a, b) => { console.log(this); // ๐๏ธ undefined // โ๏ธ error return a + b + this.num; }; console.log(obj.sum(10, 10));
Arrow functions don't have their own this keyword like named functions do.
Instead, arrow functions use the this context of the enclosing scope.
You can learn more about the related topics by checking out the following tutorials: