

- 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
Add a method to a JavaScript object constructor?
Adding a method to an object constructor is unlike adding a method to a normal object. We cannot add a method as it is the case with a normal object. To make a method in an object constructor it has to be added inside the object constructor.
Example
In the following example, the method is added inside the constructor, therefore, we have got a legitimate value.
<html> <body> <script> function Business(name, property, age, designation) { this.Name = name; this.prop = property; this.age = age; this.designation = designation; this.name = function() { return this.Name }; } var person1 = new Business("Trump", "$28.05billion", "73", "President"); document.write(person1.name()); </script> </body> </html>
Output
Trump
- Related Questions & Answers
- Add a property to a JavaScript object constructor?
- How to add a property, method to a JavaScript constructor?
- How to add a method to a JavaScript object?
- What is a constructor method in JavaScript?
- What is a constructor to create String object in JavaScript?
- Another method other than a constructor in a JavaScript class?
- How to add an element to a javascript object?
- How to create JavaScript objects using object() constructor?
- How to add 30 minutes to a JavaScript Date object?
- How to add 2 hours to a JavaScript Date object?
- How to add 10 seconds to a JavaScript date object?
- Using a JavaScript object inside a static() method?
- Accessing variables in a constructor function using a prototype method with JavaScript?
- Reference to a constructor using method references in Java8
- How to add, access JavaScript object methods?
Advertisements