
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to access a function property as a method in JavaScript?
Accessing a function as a method
A javascript object is made up of properties. To access a property as a method, just define a function to a property and include other properties in that function.
In the following example an object called "employee" is created with properties "fullName", "lastName" , "firstName" and "id". A function is defined under property "fullName" and properties such as "firstName" and "lastName" were included in it. So when the property "fullName" is called, the full name of the employee is going to display as shown in the output.
Example-1
<html> <body> <script type="text/javascript"> var employee = { firstName: "raju", lastName : "nayak", Designation : "Engineer", fullName : function() { return this.firstName + " " + this.lastName; } }; document.write(employee.fullName()); </script> </body> </html>
output
raju nayak
Example-2
<html> <body> <script type="text/javascript"> var student= { Name: "susan", country : "USA", RollNo : "5", details : function() { return "the student named" + " " + this.Name + " " +"is allocated with rollno " + " " + this.RollNo ; } }; document.write(student.details()); </script> </body> </html>
output
the student named susan is allocated with rollno 5
- Related Articles
- Access property as a property using 'get' in JavaScript?
- How to invoke a JavaScript Function as a method?
- How to invoke a function as a function and method?
- How to invoke a JavaScript Function as a function?
- How to access variables declared in a function, from another function using JavaScript?
- How to add a property, method to a JavaScript constructor?
- How to pass an object as a parameter in JavaScript function?
- Error: Permission denied to access property ‘target’ in JavaScript
- How to access nested JSON property based on another property's value in JavaScript?
- How to use JavaScript map() method to access nested objects?
- How to create a custom function similar to find() method in JavaScript?
- Passing a function as a callback in JavaScript
- How to add a property to a JavaScript object using a variable as the name?
- How to access 'this' keyword inside an arrow function in JavaScript?
- How to access object properties from result returned by async() function in JavaScript?

Advertisements