JS questions Flashcards

1
Q

Explain event delegation

A

a technique involving adding event listeners to a parent element instead of adding them to the descendant elements. The listener will fire whenever the event is triggered on the descendant elements due to event bubbling up the DOM

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

what are the benefits of event delegation?

A
  • memory footprint is smaller because only one single handler is needed
  • There is no need to unbind the handler from elements that are removed and to bind the event for new elements.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain how this works in JavaScript

A

the value of this depends on how the function is called

1) If the new keyword is used when calling the function, this inside the function is a brand new object.
2) If apply, call, or bind are used to call/create a function, this inside the function is the object that is passed in as the argument.
3) If a function is called as a method, such as obj.method() — this is the object that the function is a property of.
4) If a function is invoked as a free function invocation, meaning it was invoked without any of the conditions present above, this is the global object. In a browser, it is the window object. If in strict mode (‘use strict’), this will be undefined instead of the global object.
5) If multiple of the above rules apply, the rule that is higher wins and will set the this value.
6) If the function is an ES2015 arrow function, it ignores all the rules above and receives the this value of its surrounding scope at the time it is created.

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

what is this when using arrow functions?

A

the this value of its surrounding scope at the time it is created.

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

Explain how prototypal inheritance works

A

When a property is accessed on an object and if the property is not found on that object, the JavaScript engine looks at the object’s __proto__, and the __proto__’s __proto__ and so on, until it finds the property defined on one of the __proto__s or until it reaches the end of the prototype chain

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

What’s the difference between .call and .apply?

A

Both .call and .apply are used to invoke functions and the first parameter will be used as the value of this within the function.

.call takes in comma-separated arguments as the next arguments

.apply takes in an array of arguments as the next argument.

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