

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add properties and methods to an object in JavaScript?
To add properties and methods to an object in JavaScript, use the prototype property.
Example
You can try to run the following code to learn how to work with prototype −
<html> <head> <title>JavaScript prototype property</title> <script> function book(title, author) { this.title = title; this.author = author; } </script> </head> <body> <script> var myBook = new book("Amit", "Java"); book.prototype.price = null; myBook.price = 500; document.write("Book title is : " + myBook.title + "<br>"); document.write("Book author is : " + myBook.author + "<br>"); document.write("Book price is : " + myBook.price + "<br>"); </script> </body> </html>
- Related Questions & Answers
- How to add properties and methods to an existing object in JavaScript?
- How to add, access JavaScript object methods?
- How to add, access, delete, JavaScript object properties?
- How to create an object and access its properties in JavaScript?
- How to define methods for an Object in JavaScript?
- How to create object properties in JavaScript?
- How to delete object properties in JavaScript?
- How to duplicate Javascript object properties in another object?
- How to add an element to a javascript object?
- How to add properties from one object into another without overwriting in JavaScript?
- Extract properties from an object in JavaScript
- JavaScript Object Properties
- Remove number properties from an object JavaScript
- Merge and group object properties in JavaScript
- How to display methods and properties using reflection in C#?
Advertisements