inheritance-0.js
✓ Published0🌍 Public
Aalessioalex
Last edited Nov 22, 2014
Created on Nov 22, 2014
The example demonstrates JavaScript prototype-based inheritance through a `Person` constructor function, where `getInfoCard` is defined on `Person.prototype` and returns a formatted name-and-age string. It shows how a `Student` constructor inherits this method, allowing an instance created with `new Student('John', 18, 'MIT')` to call it successfully. It also highlights a common pitfall: invoking `Student` without `new` assigns properties to the global object and causes `getInfoCard` to fail, since the returned value is not an object with that method. No external libraries are used; the code relies solely on core JavaScript constructors, `prototype`, and `instanceof` checks (commented out).
AI-generated description