
- 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
Share methods in JavaScript
Methods can be shared by attaching them to the prototype property of the object. These methods will be shared among all the instances of the object.
Following is the code for sharing methods in JavaScript −
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; color: blueviolet; } </style> </head> <body> <h1>Shared methods in JavaScript</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to the call the displayInfo method of student1 and student2 object</h3> <script> let resEle = document.querySelector(".result"); function Student(name, age, standard) { this.name = name; this.age = age; this.standard = standard; } Student.prototype.displayInfo = function () { return `${this.name} age ${this.age} : standard :${this.standard} <br>`; }; let student1 = new Student("Rohan", 17, 12); let student2 = new Student("Shawn", 16, 11); document.querySelector(".Btn").addEventListener("click", () => { resEle.innerHTML = student1.displayInfo(); resEle.innerHTML += student2.displayInfo(); }); </script> </body> </html>
Output
On clicking the ‘CLICK HERE’ button −
- Related Articles
- JavaScript Object Methods
- Static methods in JavaScript classes?
- How to share private members among common instances in JavaScript?
- Can we share a method between JavaScript objects in an array?
- What are async methods in JavaScript?
- Shift() vs pop() methods in JavaScript?
- Explain String.trimStart() & String.trimEnd() methods in JavaScript
- How to borrow methods in JavaScript?
- JavaScript - Get Date Methods
- JavaScript Set Date Methods
- JavaScript Basic Array Methods
- Checking if decimals share at least two common 1 bits in JavaScript
- What are async generator methods in JavaScript?
- Adding methods to Javascript Prototypes
- Difference between shift() and pop() methods in Javascript

Advertisements