Computer >> Computer tutorials >  >> Programming >> Javascript

How to invoke a JavaScript Function as a method?


Functions in JavaScript can be easily assigned to object properties. You can also invoke it using parentheses ().

Example

You can try to the following code to invoke a JavaScript function as a method −

<html>
   <head>
      <script>
         var employee = {'empname': 'David'}
         employee.name = function() {
            return this.empname;
         }

         document.write(employee.name());
      </script>
   </head>
</html>