GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaScript/035-Construction-function-Inheritance.js at main · jaydattpatel/JavaScript Expand file tree/ Copy path035-Construction-function-Inheritance.jsMore file actions 90 lines (76 loc) · 2.41 KB / Copy path035-Construction-function-Inheritance.jsFile metadata and controls90 lines (76 loc) · 2.41 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 /* Author : Jaydatt Patel Inherit constructor function. We can also use constructor function to create new derived constructor function same as base super class and sub or derived class. */ console.log("---------------------1-----------------------"); function Vehicle(name, color, wheels) { this.name = name; this.color = color; this.wheels = wheels; this.showV = function () { console.log(this); }; } Vehicle.prototype.getVeh = function () { console.log( `Name: ${this.name}, Color: ${this.color}, Wheels: ${this.wheels}` ); }; console.log(Object.create(Vehicle)); function Car(color, brand, seats) { Vehicle.call(this, "car", color, 4); this.brand = brand; this.seats = seats; } // copy all prototype which are inherited in base function Object.setPrototypeOf(Car.prototype, Vehicle.prototype); // Car.prototype = Vehicle.prototype; // or Car.prototype = Object.create(Vehicle.prototype); //add seperate prototype Car.prototype.getDetails = function () { console.log( `The ${this.color} ${this.name} is of ${this.brand} company. It has seating for ${this.seats} people` ); }; const car1 = new Car("Black", "Audi", 5); console.log("car1 : ", car1); console.log("car1.__proto__ :", car1.__proto__); car1.getDetails(); // calling inherited method of derived function. car1.getVeh(); // calling inherited method of base function console.log("---------------------2-----------------------"); function Book(name, pages, price) { this.name = name; this.pages = pages; this.price = price; this.bookInfo = function () { console.log( `name: ${this.name}, pages: ${this.pages}, price: ${this.price}` ); }; } function Author(name, pages, price, authorName) { Book.call(this, name, pages, price); // calling function call this.authorName = authorName; this.authorInfo = function () { console.log(`authorName: ${this.authorName}`); }; } function Motivation_Books(name, pages, price, authorName, details) { Book.call(this, name, pages, price); // calling function call Author.call(this, name, pages, price, authorName); // calling function call this.details = details; this.getInfo = function () { console.log(this); }; } let book1 = new Motivation_Books( "XYZ", 200, "1500 INR", "Mr.Author", "New Book For Moto" ); book1.getInfo(); book1.bookInfo(); book1.authorInfo(); |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |