JavaScript Flashcards

1
Q

Explain prototypes in JavaScript.

A

When you call any property on any JavaScript object, the interpreter will first look for that property in the object itself. If it does not find it there, it will look in the object’s prototype (which is pointed to by the object’s internal __proto__ property).

If it does not find the property in the prototype, it will recursively look at the prototype’s __proto__ property to continue up the prototype chain. How does the chain stop? Object.prototype.__proto__ == *null, so eventually the chain ends.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain closures

A

Closure allows a function to access variables from an enclosing scope — environment — even after it leaves the scope in which it was declared. Closures allow for factory functions.

function dwightJob(title) {
    return function(prefix) {
        return prefix + ' ' + title;
    };
}
var sales = dwightJob('Salesman');
var manager = dwightJob('Manager');

alert(sales(‘Top’)); // Top Salesman

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain Context

A

Refers to how a function is invoked, and refers to ‘this’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain scope

A

visibility of variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain call, apply, bind

A

All 3 are used to attach ‘this’ into function

var say = function(greeting){
    alert(greeting + ', ' + this.name);
};
//person1 becomes 'this'
say.call(person1, 'Hello');

1) call is used list of argument
2) apply is for array of arguments
3) bind returns a new function, with a certain context and parameters. It is usually used when you want a function to be called later with a certain context.

const Snow = {surename: 'Snow'}
const char = {
  surename: 'Stark',
  knows: function(arg, name) {
    console.log(`You know ${arg}, ${name} ${this.surename}`);}
  }
const whoKnowsNothing = char.knows.bind(Snow, 'nothing');
whoKnowsNothing('Jon');  // You know nothing, Jon Snow
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain EventLoop

A

JavaScript is single-threaded. Functions are processed using a callstack. Async functions need to be processed a little bit differently. JavaScript moves Async functions to a task-queue and from there it sits until the entire callstack is empty. Then JS dequeues and processes the async functions.
Pros: Non-blocking. Browser doesn’t freeze while events are processing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Function Declaration vs Function Expression?

A

Function Expression:
var myFunction = function name {
statements
};

Function Declaration:
function sum (a,b){
 return  a + b;
}

Expression allows for annonymous functions to exist. Pros: Can be used as IIFE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is this in JS? What are some common pitfalls with this?

A

‘this’ refers to the current context. In the global scope, this refers to the window. ‘this’ can also reference the context inside a function or object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

== vs. ===

A

== is equality with co-ersion. === checks for strict equality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly