Do you think JavaScript Functions are Object Methods?

JavascriptWeb DevelopmentFront End Technology

Methods in JavaScript can be said as an action, which is performed on objects. It is a property like an employee name, department, eid, etc with properties “Amit”, “Marketing”, “001”, etc.

To access object methods in JavaScript −

object.method()

Let’s see an example,

employee.details()

Example

You can try to run the following code to access object methods −

 Live Demo

<html>
   <head>
      <script>
         var employee = {
            empname: "Amit",
            department : "Marketing",
            id : 001,
            details : function() {
               return this.empname + " with ID: " + this.id;
            }
         };
         document.write(employee.details());
      </script>
   </head>
</html>

Output

raja
Published on 23-Feb-2018 11:57:12
Advertisements