And how to use it
Developer Advocate at Dynatrace
And how to use it
Developer Advocate at Dynatrace
Explain Inheritance to a 5 year old
Sure Let’s say you have a toy box that has toys inside it. The toy box is like a class in programming … … …
— ChatGPT: AI slightly better than B**d
Inheritance is the process by which genetic information is passed on from parent to child.
In Programming: Inheritance is the process by which features are passed on from parent to child.
class SmartPhone { public: void captureImages() {} }; class Iphone: public SmartPhone { public: void faceIDScan() {} }; // creating object of Iphone Iphone x; // calling method on object x.faceIDScan(); // calling an inherited method x.captureImages(); // calling unknown method x.blablabla(); // ❌ error
class SmartPhone { public: void captureImages() {} }; class Iphone: public SmartPhone { public: void faceIDScan() {} }; // creating object of Iphone Iphone x; // calling method on object x.faceIDScan(); // calling an inherited method x.captureImages(); // calling unknown method x.blablabla(); // ❌ error
const greeting = { message: "Welcome to freeCodeCamp!" }; // accessing properties greeting.message; // Welcome to freeCodeCamp! // where does this comes from? greeting.toString();
const greeting = { message: "Welcome to freeCodeCamp!" }; // accessing properties greeting.message; // Welcome to freeCodeCamp! // where does this comes from? greeting.toString();
const obj = {};
const obj = {};
const obj = new Object();
const obj = new Object();
Object.create
methodObject.create(prototype); // returns object with given prototype // prototype object let SmartPhone = { captureImages: function() {} }; let iPhone = Object.create(SmartPhone); iPhone.captureImages(); // works
Object.create(prototype); // returns object with given prototype // prototype object let SmartPhone = { captureImages: function() {} }; let iPhone = Object.create(SmartPhone); iPhone.captureImages(); // works
function Rectangle(height, width) { this.height = height; this.width = width; } Rectangle.prototype.calcArea = function() { return this.height * this.width; } const rectangle = new Rectangle(10, 20);
function Rectangle(height, width) { this.height = height; this.width = width; } Rectangle.prototype.calcArea = function() { return this.height * this.width; } const rectangle = new Rectangle(10, 20);
🧠 Why use prototype?
class
keywordclass Rectangle { constructor(height, width) { this.height = height; this.width = width; } calcArea() { return this.height * this.width; } } const rectangle = new Rectangle(10, 20);
class Rectangle { constructor(height, width) { this.height = height; this.width = width; } calcArea() { return this.height * this.width; } } const rectangle = new Rectangle(10, 20);
extends
class Parent { scold() {} } class Child extends Parent { haveFun() {} } const child = new Child();
class Parent { scold() {} } class Child extends Parent { haveFun() {} } const child = new Child();
[[Prototype]]
const obj = {}; // standard Object.getPrototypeOf(obj); // non-standard obj.__proto__;
const obj = {}; // standard Object.getPrototypeOf(obj); // non-standard obj.__proto__;
const object1 = {"property1": 42}; console.log(object1.hasOwnProperty('property1')); // Expected output: true console.log(object1.hasOwnProperty('toString')); // Expected output: false
const object1 = {"property1": 42}; console.log(object1.hasOwnProperty('property1')); // Expected output: true console.log(object1.hasOwnProperty('toString')); // Expected output: false