w5d5/w6d1 - javascript Flashcards

1
Q

What are the falsy values in JS?

A
-1
0
""
undefined
NaN
false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Does JS have implicit return?

A

no

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

What are the 5 JS primitives?

A
integer
string
boolean
undefined
null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does var scoping/reassignment work?

A

available within the function it is defined in

can be reassigned

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

How does let scoping/reassignment work?

A

block scoped

can be reassigned

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

How does const scoping/reassignment work?

A

block scoped

cannot be reassigned

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

Can you use a variable before it is defined?

A

Yes.

It must be defined in the same scope.

This only works for var/let. Does not work for const.

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

What’s the story with constructor functions?

A

ProudCamelCased

sort of like declaring a class

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

What are the different types of function invocation?

A

method-style: the current ‘this’ will be the enclosing scope.

constructor-style: ‘this’ will be enclosed by the constructor method

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

What does ‘this’ refer to?

A

The current context.

Roughly equivalent to ruby’s self.

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

How do you get a function in JS to implicitly return?

A

Make it a one-line fat-arrow function.

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

How does string interpolation work

A

backticks for quotes

${ … } to interpolate …

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

Fat arrow functions are like lambdas in which way?

A

They return execution to the context in which they are defined.

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

What happens if you send extra params to a method?

A

They are unused.

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

How do you access all arguments passed to a function?

A

They can be accessed via the ‘arguments’ array-like object available within the method

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

How do you treat arguments like an array?

A

arguments_as_an_actual_array = Array.prototype.slice.call(arguments)

OR (ES6)

Array.from(arguments)

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

With function-style invocation, what is ‘this’ set to?

A

window

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

With method-style invocation, what is ‘this’ set to?

A

the object that we call the method on

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

With constructor-style invocation, what happens behind the scenes? What is ‘this’ set to?

A

a new, empty object is created.

the object’s __proto__ property is set to Classname.prototype

the Classname function is set with ‘this’ set to the blank object.

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

Where is apply defined, what does it take as parameters and what does it do?

A

Function.prototype.apply

it takes an object to bind ‘this’ to, and it takes an array of arguments to be passed to the method it is being called on

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

What’s the difference between apply and call?

Where would you use each?

A

call takes a list of parameters, while apply takes an array.

use call when you know specifically which parameters you will be passing.

use apply when someone will give you an array of arguments to apply.

22
Q

How do you set up a class using ES2015 sugar?

A
class MyClass {
constructor(arg1, arg2, ...) {
  this.arg1=arg1
..
}

method1( ) { … }
}

23
Q

Why do we assign a variable to the return value of require using node?

A

In JS, we don’t import a module’s global scope, so we have to store it into a variable.

24
Q

How do we export a Class from a .js file in node?

A
// ./cat.js
module.exports = ClassName
// ./main.js
require("./cat.js")
25
Q

How do we import many js files from the same directory?

A

Put a bunch of .js files in the same dir, add them to an index.js file, and require the dir

// ./things/index.js
module.exports = {
Thing1: require("./thing1"),
Thing2: require("./thing2"),
...
}
// ./main.js
const Things = require("./things")
26
Q

What’s a callback

A

a function passed to another function

27
Q

What’s the JS equivalent of Ruby’s splat operator?

A

… (the spread operator)

28
Q

What are three ways to change the context of a function?

A

apply, call, bind

29
Q

What happens when you rebind a function?

A

Nothing. The first bind is the only bind.

30
Q

How do you use a closure to call a function (which is defined in a class) via method-style invocation?

A
  1. Within the closure, store that = this.
  2. Invoke the function via that.theMethod()

OR

use a fat-arrow function because it automatically binds ‘this’ to the defining scope as it is defined

31
Q

How do you call a function asynchronously?

A

use a built-in method like:

setTimeout, setInterval, …

32
Q

What is the top-level class in JS? Why do we call it this?

A

Object

When a property is called on a JS object, the interpreter looks first for that property in the object itself, then the object’s prototype, then recursively, all the way up to Object.

33
Q

What are 3 ways to cause a a Dog object to inherit from an Animal object?

A

Dog.prototype.__proto__ = Animal.prototype (bad because slow)

OR

Object.setPrototypeOf(Dog.prototype, Animal.prototype) – bad because altering an object’s prototype reduces its performance

OR

Dog.prototype = Object.create(Animal.prototype)

best, fastest, recommended

34
Q

How do we set up a Dog’s constructor to call an Animal’s constructor (in the case that Dog inherits from Animal) ?

A

Within function Dog(name, breed), call:

Animal.call(this, name)

Note that we only pass in arguments that are taken by Animal’s constructor.

35
Q

How do you set up inheritance between a Dog and Animal via a surrogate?

A

function Surrogate( ) { }

Surrogate.prototype = Animal.prototype

Dog.prototype = new Surrogate( );

36
Q

How does inheritance in ES2015 work?

A
class Dog extends Animal {
  constructor(a, b, c) { ... }
  action( ) { ... }
}
37
Q

Why would you ever run code with setTimeout(…, 0) ?

A

To run something AFTER the current call stack is cleared, but not before.

38
Q

Does setTimeout(…, 1000) guarantee that the event will be triggered in 1 second?

A

No. 1000 is the minimum delay.

39
Q

How do you set up a function to set up inheritance between a parent and a child?

var inherit = function(parent, child) { … }

(implement the three dots)

A
var Surrogate = function( ) { };
Surrogate.prototype = parent.prototype;
child.prototype = Surrogate.prototype;
child.prototype.constructor = child;
40
Q

What is the effect of running this code? Why is the last line necessary?

var Surrogate = function( ) { };
Surrogate.prototype = parent.prototype;
child.prototype = Surrogate.prototype;
child.prototype.constructor = child;
A

Inheritance is set up between parent and child.

Prior to running the last line, the child’s constructor is set to the parent’s constructor. The last line changes it so the child constructor is called when we create a new instance of child.

41
Q

How would you draw a rectangle using an existing canvas, WITHOUT using the .rect( ) method?

specs:

  • Black outline 2px
  • red insides
  • 50x50
  • top-left corner of (50,50)
A

ctx = canvas.getContext(‘2d’);

ctx. strokeStyle = ‘black’;
ctx. lineWidth = 2;
ctx. fillStyle = ‘red’;

ctx. beginPath( );
ctx. moveTo(50, 50);
ctx. lineTo(50, 100);
ctx. lineTo(100, 100);
ctx. lineTo(100, 50);
ctx. closePath( );

ctx. stroke( );
ctx. fill( );

42
Q

How would you explain .bind( ) in terms of closures?

A

bind creates a closure that contains its parameter.

when invoked, the bound method will be called via call with ‘this’ set to the bind parameter.

43
Q

Name the one variable that does not get captured

A

this

44
Q

What’s the difference between a statement and an expression?

A

An expression is a line of code that returns a value.

A statement is any line of code.

So, an expression is a statement, but a statement is not necessarily an expression.

45
Q

How do you make a single-expression fat arrow return an empty object rather than an empty block?

A

myFunc = ( ) => ( { } );

46
Q

What will this return?

myFunc = ( ) => ( { } );

A

An empty object.

47
Q

What will this return?

myFunc = ( ) => { };

A

An empty block.

48
Q

Explain why the result of this function call is undefined:

let returnName = ( ) => this.name;
returnName.call( {name: 'Dale Cooper'} ) // undefined;
A

Fat arrow functions pre-bind ‘this’ to the scope in which they are defined.

With this example, we’re trying to rebind ‘this’ to an object with a ‘name’ variable, which we can’t do because .bind has already been performed via the fat-arrow function.

49
Q

How do you access the arguments object within a fat-arrow function?

A

You don’t.

50
Q

What does .bind return?

A

A function that calls the original function from a specified context.

51
Q

What does .push return? What doesn’t .push return?

A

It returns the length of the elongated array.

It doesn’t return the elongated array.

52
Q

How do you import everything from a file and access it via dot syntax?

A

import * as AwesomeStuff from ‘someFile’;

AwesomeStuff.someFunction( );