-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInheritance.js
92 lines (71 loc) · 2.63 KB
/
Inheritance.js
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
91
92
//* Inheritance - JavaScript
/* In JavaScript, inheritance is achieved through prototypes. Each object has an internal prototype that
It could be another object. When you try to access a property of an object that is not present on it,
JavaScript will look for that property in the object's prototype. This forms the basis of the inheritance
system in JavaScript. Here is a basic example: */
//? Prototypes and Inheritance:
// Define an object constructor
function Animal(name) {
this.name = name;
}
// Add a method to the prototype
Animal.prototype.greet = function() {
console.log("Hello, I am " + this.name);
};
// Create an object that inherits from Animal
function Cat(name, color) {
Animal.call(this, name);
this.color = color;
}
// Establish the prototype chain
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.constructor = Cat; // Restore the constructor
// Add an additional method to the Cat prototype
Cat.prototype.meow = function() {
console.log("Meow");
};
// Create object instances
let myAnimal = new Animal("Fluff");
let myCat = new Cat("Mittens", "gray");
myAnimal.greet(); // Displays "Hello, I'm Pelusa"
myCat.greet(); // Displays "Hello, I'm Mittens"
myCat.meow(); // Displays "Meow"
//? In this example:
//* - `Animal`
// It is an object constructor that has a property `name` and a method `greet`.
//* - `Cat`
/* is another constructor that inherits from `Animal`. `Object.create` is used to set the prototype
`Cat` as a new instance of `Animal`. */
// - Restore the constructor of `Cat` to make sure it is `Cat` and not `Animal`.
//* - `myCat`
// inherits the properties and methods of `Animal` and has its own `meow` method.
//? Classes in EcmaScript 6:
/* EcmaScript 6 introduced class syntax, which provides a simpler way to achieve inheritance
in JavaScript. */
class Animal {
constructor(name) {
this.name = name;
}
greet() {
console.log("Hello, I am " + this.name);
}
}
class Cat extends Animal {
constructor(name, color) {
super(name);
this.color = color;
}
meow() {
console.log("Meow");
}
}
let Animal = new Animal("Fluff");
let Cat = new Cat("Mittens", "gray");
animal.greet(); // Displays "Hello, I'm Pelusa"
Cat.greet(); // Displays "Hello, I'm Mittens"
Cat.meow(); // Displays "Meow"
/*In this example, `class` and `extends` are used to define classes and inheritance more
declarative. */
/* Both forms, prototype-based inheritance and EcmaScript 6 classes, are common in JavaScript.
Choosing between them often depends on the developer's preferences and requirements.
project specific. */