Explain JavaScript Prototypal Inheritance.
How Implementing JavaScript inheritance using extends and super.
How Implementing JavaScript Getters and Setters.
- Explain JavaScript Prototypal Inheritance.
The Object.create() method creates a new object and uses an existing object as a prototype of the new object:
Object.create(proto, [propertiesObject])
Code language: JavaScript (javascript)
The Object.create() method accepts two arguments:
The first argument (proto) is an object used as the prototype for the new object.
The second argument (propertiesObject), if provided, is an optional object that defines additional properties for the new object.
Suppose you have a person object:
let person = {
name: "John Doe",
greet: function () {
return "Hi, I'm " + this.name;
}
};
Code language: JavaScript (javascript)
The following creates an empty teacher object with the proto of the person object:
let teacher = Object.create(person);
Code language: JavaScript (javascript)
After that, you can define properties for the teacher object:
teacher.name = 'Jane Doe';
teacher.teach = function (subject) {
return "I can teach " + subject;
}
Code language: JavaScript (javascript)
Or you can do all of these steps in one statement as follows:
let teacher = Object.create(person, {
name: { value: 'John Doe' } ,
teach: { value: function(subject) {
return "I can teach " + subject;
}}
});
36.How Implementing JavaScript inheritance using extends and super.
function Animal(legs) {
this.legs = legs;
}
Animal.prototype.walk = function() {
console.log('walking on ' + this.legs + ' legs');
}
function Bird(legs) {
Animal.call(this, legs);
}
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Animal;
Bird.prototype.fly = function() {
console.log('flying');
}
var pigeon = new Bird(2);
pigeon.walk(); // walking on 2 legs
pigeon.fly(); // flying
Code language: JavaScript (javascript)
ES6 simplified these steps by using the extends and super keywords.
The following example defines the Animal and Bird classes and establishes the inheritance through the extends and super keywords.
class Animal {
constructor(legs) {
this.legs = legs;
}
walk() {
console.log('walking on ' + this.legs + ' legs');
}
}
class Bird extends Animal {
constructor(legs) {
super(legs);
}
fly() {
console.log('flying');
}
}
let bird = new Bird(2);
bird.walk();
bird.fly();
37.How Implementing JavaScript Getters and Setters.
The following example defines a class called Person:
class Person {
constructor(name) {
this.name = name;
}
}
let person = new Person("John");
console.log(person.name); // John
Code language: JavaScript (javascript)
The Person class has a property name and a constructor. The constructor initializes the name property to a string.
Sometimes, you don’t want the name property to be accessed directly like this:
person.name
Code language: CSS (css)
To do that, you may come up with a pair of methods that manipulate the name property. For example:
class Person {
constructor(name) {
this.setName(name);
}
getName() {
return this.name;
}
setName(newName) {
newName = newName.trim();
if (newName === '') {
throw 'The name cannot be empty';
}
this.name = newName;
}
}
let person = new Person('Jane Doe');
console.log(person); // Jane Doe
person.setName('Jane Smith');
console.log(person.getName()); // Jane Smith
Top comments (0)