wip: add more questions

chore/javascript
Arik Chakma 1 year ago
parent 1dd53d8994
commit 56473b129c
  1. 18
      src/data/question-groups/javascript/content/iife.md
  2. 38
      src/data/question-groups/javascript/content/inheritance.md
  3. 10
      src/data/question-groups/javascript/javascript.md

@ -0,0 +1,18 @@
The IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.
```js
(function () {
console.log('Hello Roadmap!');
})();
```
The IIFE is frequently used to create a new scope to avoid variable hoisting from within blocks.
```js
(function () {
var roadmap = 'JavaScript';
console.log(roadmap);
})();
console.log(roadmap); // ReferenceError: name is not defined
```

@ -0,0 +1,38 @@
Inheritance is a way to create a new `Class` from an existing `Class`. The new `Class` inherits all the properties and methods from the existing `Class`. The new `Class` is called the child `Class`, and the existing `Class` is called the parent `Class`.
## Example
```js
class Roadmap {
constructor(name, description, slug) {
this.name = name;
this.description = description;
this.slug = slug;
}
getRoadmapUrl() {
console.log(`https://roadmap.sh/${this.slug}`);
}
}
class JavaScript extends Roadmap {
constructor(name, description, slug) {
super(name, description, slug);
}
greet() {
console.log(`${this.name} - ${this.description}`);
}
}
const js = new JavaScript(
'JavaScript Roadmap',
'Learn JavaScript',
'javascript'
);
js.getRoadmapUrl(); // https://roadmap.sh/javascript
js.greet(); // JavaScript Roadmap - Learn JavaScript
```
In the above example, the `JavaScript` class inherits the `getRoadmapUrl()` method from the `Roadmap` class. This is because the `JavaScript` class extends the `Roadmap` class using the `extends` keyword. In the `JavaScript` class, the `getRoadmapUrl()` method is not found, so JavaScript looks up the prototype chain and finds the `getRoadmapUrl()` method in the `Roadmap` class.

@ -103,4 +103,14 @@ questions:
topics: topics:
- 'OOP' - 'OOP'
- 'Advanced' - 'Advanced'
- question: What is IIFE in JavaScript?
answer: iife.md
topics:
- 'Core'
- 'Advanced'
- question: What is Inheritance in JavaScript?
answer: inheritance.md
topics:
- 'OOP'
- 'Advanced'
--- ---

Loading…
Cancel
Save