How to invoke a JavaScript Function as a method?

JavascriptWeb DevelopmentFront End Technology

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>
raja
Published on 23-Feb-2018 07:54:04
Advertisements