Learn JavaScript
this on Objects
39.1
Lesson

Function as a property of an object

We can store a function as the property of an object. Such functions are called methods of the object.

In the example given below, we are using a function expression to create the function and assign it to the property admin.sayHello. Here, sayHello is the method of the admin object.

We can also declare a function before assigning it to a property.

In the example given above, we first declare the sayHello function. Then we assign it to the greetings property.