- 1. Explain event delegation
- 2. Explain how this works in JavaScript
- 3. Explain how prototypal inheritance works.
- 4. What is a promise? Where and how would you use promise?
- 5. What is the difference between while and do...while loops in JavaScript?
- 6. Why you might want to create static class members?
- 7. How can you share code between files?
1. Explain event delegation
2. Explain how this
works in JavaScript
Global context
In the global execution context (outside of any function), this
refers to the global object.
1 | // In web browsers, the window object is also the global object: |
Function context
Inside a function, the value of this
depends on how the function is called.
In non-strict mode, the value of this
defaults to the global object, which is window
in a browser.
1 | function f1() { |
In strict mode, if the value of this
is not set when entering an execution context, it remains as undefined
.
1 | function f2() { |
Class context
The behavior of this
in classes and functions is similar.
Within a class constructor, this
is a regular object. All non-static methods within the class are added to the prototype of this
.
1 | class Example { |
Arrow functions
In arrow functions, this
retains the value of the enclosing lexical context’s this
.
3. Explain how prototypal inheritance works.
4. What is a promise? Where and how would you use promise?
A promise is an object that my produce a single value some time in the future: either a resolved value, or a reason that’s not resolved. A promise may be in one of 3 possible states: fufilled, rejected, or pending. It can be chained with callback functions to handle the fufilled value or the reason for rejection.
- Complex async code made easier
- Promisifying XMLHttpRequest
- Chaining
then
s to transform values or run additional async actions one after another
5. What is the difference between while
and do...while
loops in JavaScript?
The difference is a matter of when the condition is checked.
- A
while
loop checks the condition, then executes the loop. - A
do...while
loop executes the loop and then checks the conditions.
6. Why you might want to create static class members?
Static properties are used when we’d like to store class-level data, also not bound to an instance.
Static properties and methods are inherited.
7. How can you share code between files?
Solution 1. Global object
utils.js
1 | var add = function(num1, num2){ |
file1.js
1 | globalobject.add(4, 4); |
Solution 2. CommonJS
utils.js
1 | var add = function(num1, num2){ |
file1.js
1 | var { add, subtract } = require('./utils.js'); |
*Solution 3. ESM
utils.js
1 | var add = function(num1, num2){ |
file1.js
1 | import globalobject from './utils.mjs' |
Solution 4. JSONP
utils.js
1 | var add = function(num1, num2){ |
file1.js
1 | function jsonp(file, callback) { |
8.Explain the difference between synchronous and asynchronous functions.
Synchronous functions: It waits for each operation to complete, after that only it executes the next operation.
Asynchronous functions: It never waits for each operation to complete, rather it executes all operations in the first only. The result of each operation will be handled once the result is available.