Adding a Method to a Constructor You cannot add a new method to an object constructor the same way you add a new method to an existing object. Use the following example to add a method to a JavaScript object constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <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> |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.