Javascript Flashcards

1
Q

What does eventpreventDefault() do?

A

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur.

For example, this can be useful when:

  • Clicking on a “Submit” button, prevent it from submitting a form
  • Clicking on a link, prevent the link from following the URL

Note: Not all events are cancelable. Use the cancelable property to find out if an event is cancelable.

Note: The preventDefault() method does not prevent further propagation of an event through the DOM. Use the stopPropagation() method to handle this.

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

Explain how this works in JavaScript

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

What’s the difference between a variable that is: null, undefined or undeclared? How would you go about checking for any of these states?

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

What is a closure, and how/why would you use one?

A

A closure is the combination of a function and the lexical environment within which that function was declared. The word “lexical” refers to the fact that lexical scoping uses the location where a variable is declared within the source code to determine where that variable is available. Closures are functions that have access to the outer (enclosing) function’s variables—scope chain even after the outer function has returned.

Why would you use one?

  • Data privacy / emulating private methods with closures. Commonly used in the module pattern.
  • Partial applications or currying.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the difference between host objects and native objects?

A

Native objects are objects that are part of the JavaScript language defined by the ECMAScript specification, such as String, Math, RegExp, Object, Function, etc.

Host objects are provided by the runtime environment (browser or Node), such as window, XMLHTTPRequest, etc.

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

Explain Function.prototype.bind

A

Taken word-for-word from MDN:

The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

In my experience, it is most useful for binding the value of this in methods of classes that you want to pass into other functions. This is frequently done in React components.

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

Explain how this works in JavaScript

A
  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
8
Q

Explain how prototypal inheritance works

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