// Asked in servicenow and AngleOne
// Explain how prototypal inheritance works in JavaScript
// and how a child inherits properties from a parent using prototype.
// This can be easily done using a class-based but to judge your prototype knowledge
// such questions can come.
Person.prototype.hello = function() {
return `Hello ${this.name}`;
function Developer(name, title) {
// Override person prototype in Developer's prototype
Developer.prototype = Object.create(Person.prototype);
// Reset the constructor property of Developer's prototype
Developer.prototype.constructor = Developer;
// Now you can add any methods in developer prototype
Developer.prototype.getTitle = function() {
const obj = new Developer("Alice", "Software Engineer");
console.log(obj.hello()); // Output: Hello Alice
console.log(obj.getTitle()); // Output: Software Engineer
// Learn more here about - https://www.youtube.com/watch?v=CpmE5twq1h0
// https://youtu.be/wstwjQ1yqWQ?si=gXOhIy4v_9NELs-j