
- 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 add, access JavaScript object methods?
Following is the code to add, access JavaScript object methods −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>Add, Access JavaScript object methods</h1> <div style="color: green;" class="result"></div> <button class="Btn">CHECK</button> <h3>Click on the above button to add and access object methods.</h3> <script> let resEle = document.querySelector(".result"); let obj = { firstName:'Rohan', lastName: 'Sharma', Age:'22', } document.querySelector(".Btn").addEventListener("click", () => { obj.welcome = function(){return 'Welcome '+this.firstName+' '+this.lastName}; resEle.innerHTML = 'Adding function welcome : ' + obj.welcome + ''; resEle.innerHTML += 'Accessing function welcome : ' + obj.welcome() + ''; }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CHECK’ button −
- Related Articles
- How to add, access, delete, JavaScript object properties?
- How to add properties and methods to an object in JavaScript?
- How to add properties and methods to an existing object in JavaScript?
- How to access an object through another object in JavaScript?
- JavaScript Object Methods
- How to access methods of an array of objects in JavaScript?
- How to access cookies using document object in JavaScript?
- How to access a JavaScript object using its own prototype?
- How to define methods for an Object in JavaScript?
- How to access an object value using variable key in JavaScript?
- How to create an object and access its properties in JavaScript?
- How to access the first value of an object using JavaScript?
- How to add an element to a javascript object?
- How to add a method to a JavaScript object?
- How to access an object having spaces in the object’s key using JavaScript?

Advertisements