Javascript Patterns Flashcards

1
Q

What is the default value of anything in javascript that has not been defined?

A

Undefined. null has to be set explicitly

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

Why do primitive types in js have methods?

A

Because everything in js can be treated as an object. The js runtime wraps primitive types in an Object wrapper when it is evaluated. e.g. calling the (1).toPrecision(3) method on the number one, will wrap the number in a Number object.

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

What types are supported by js?

A

Number, String, Object, Boolean, undefined, null, and Symbol.

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

In js, integer, short, long, double and float are all represented by what type?

A

Number

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

difference between var, let, const

A

Scope: var is function scoped, let and const are block scoped

Definition and assignment: var can be redefined and re-assigned, let can be re-assigned, but not redefined, const cannot be redefined or re-assigned.

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

What is meant by “functions are first-class citizens”?

A

In programming languages, when you are able to pass, return and assign a type, that type is considered to be a first class citizen

Functions are objects and can be assinged to variables, passed into other functions as arguments and also be returned from other functions

Functions inherits from the Object prototype and supports all the operational properties inherent to other entities

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

Difference between classical and prototypal inheritance?

A

Objects are abstractions of physical real-world objects.

Classes are a Generalization of an object. In other words, Classes are an abstraction of an object of a real world thing. (Classes, then, are an abstraction of an abstraction of a real-world thing). Since a Class is yet another reference to (or abstraction of) its predecessor, each descendant child Class increases the level of abstraction, thus increasing the level of generalization.

As opposed to Classical Inheritance, Prototypal Inheritance does not deal with increasing layers of abstraction. An Object is either an abstraction of a real-world thing, same as before, or is a direct copy of another Object (in other words, a Prototype). Prototypal inheritance is accomplished through an Object linking mechanism.

This is important! It means that Generalizations are just other Objects. With Classical Inheritance, Generalizations are abstractions of abstractions of abstractions… all the way down to the most recent descendant.

The abstraction level here is never deeper than 1; The only abstraction that occurs with Prototypal Inheritance is the abstraction away from real-world things.

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

What are prototypes?

A

A prototype is an abstraction of an object

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

What is a class?

A

A generalization of an object

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

What is an object?

A

A javascript object is an abstraction of a real-world or physical object.

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

What is a prototypal chain?

A

a

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

Difference between for/in and for/of

A

Both are used to loop through objects. For in loops through property names, for of loops through property values.

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

difference between a set and an array?

A

sets dow’t allow duplicate values

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

Function expression

A
//assigned to a variable
const someFunct = function(){ //some code here }

//assigned to a property within an object

{
someFunct: function(){//some code here}
}

//lamda -passed in as an argument 
const someFunct(function(x){//some code here}){
     //some code here
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Function declaration

A

function someFunct(){//some code here}

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

What are the two ways to write a function

A

function declaration or function expression

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

What do you need to keep in mind when declaring function expressions?

A

function expressions uses anonymous functions that don’t have their own name,and when stored in other variables is subject to the same hoisting rules that apply to variables

18
Q

What is hoisting?

A

because var variables are function scoped, the variables are hoisted (moved to the top of the function body) at runtime. Thus, var variables declared within blocks inside the function will be hoisted and have not only block scope but also function scope.

19
Q

Why do javascript developers declare variables at the top of the function body?

A

To be more explicit about hoisting, as to avoid confusion and errors

20
Q

const variables and objects

A

Although const variables cannot be reassigned, when a const variable points to an object, the object properties can be updated without generating an exception

21
Q

Difference between anonymous and named function expressions.

A

Strict anonymous functions are not bound to an identifier, which means that it won’t show up in the callstack trace, making it difficult to debug. Strict Anonymous functions also cannot be used in recursion as without a name they can’t call themselves.

named functions are function expressions that have been bound to an identifier, by adding a name after the function keyword, in which case it will show up in the stack strace when an error occurs.

22
Q

Two kinds of function expressions

A

named and anonymous.

23
Q

Lexical Scope

A

Lexical Scope is the scoping scheme used in JavaScript where every inner scope has access to the outer scopes.

24
Q

A closure

A

A closure is a function that encloses or captures variables from its surrounding environment.

25
Q

Why should anonymous functions be avoided

A

Anonymous function expressions are less readable, harder to debug and harder to use in recursion.

26
Q

What can’t you do with a named function expression?

A

Call them using their own names, outside of their function bodies. Their own names are used as internal identifiers and can only be used from within their own function bodies.

27
Q

What advantages do function declarations have over function expressions?

A

They are named, and you can use that name from outside and from within the function. They are not hoisted as variables but as a whole, which makes them impervious to hoisting problems.

28
Q

What does the .hasOwnProperty() method do?

A

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it)

29
Q

REPL

A

Read, Evaluate, Print, Loop

30
Q

ES 6 Native method for merging defaults?

A

Object.assign({}, details,defaults)

31
Q

Method for coding fallback values

A

var someVariable = newValue || defaultValue

32
Q

MergeDefaults Pattern

A
function(details,defaults){
for( let props in defaults){
     if(defaults.hasOwnProperty(props) && !details[props]){
            details[props] = defaults[props]
     }
}

return details
}

33
Q

3 Ways to merge defaults in javascript

A
  1. Use fallback values
  2. ES 6 method - Object.assign(details,defaults)
  3. Use the mergeDefaults pattern
34
Q

arity

A

arity is the amount of arguments a function expects. A function with an arity of 3, expects three arguments.

The arity of a function can be seen by printing functionName.length;

35
Q

is it possible to call a function without the specified amount of arguments, or no arguments, more arguments than specified?

A

Yes, however, depending on how those arguments are used in the body of the function, the function might generate an exception or error

36
Q

What is the arguments object within functions?

A

Every function has an arguments object, which is an array-like object (not an array) that contains the arguments that was passed into the function at runtime.

One can access these arguments through the arguments object

37
Q

In which kind of function is the arguments object not found?

A

ES 6 Arrow functions

38
Q

How to turn the arguments object into an true array?

A
  1. let argsArray = Array.prototype.slice.call(arguments);

2. let argsArray = Array.from(arguments)

39
Q

What is duck typing?

A

Javascript cares about what objects can do, not their type

“If it walks like a duck and talks like a duck, then it IS a duck”

Javascript doesn’t care if it is an actual duck as long as the object’s interface behaves like one.

This is related to the concept of polymorphism

40
Q

How are array-like objects different from arrays?

A

They look like arrays, but they are not. You can index them, enumerate them and access their length property but the similarity ends there as they don’t inherit from ` Array.prototype’.

41
Q

How to handle an arbritary amount of arguments in functions?

A
  1. By accessing the arguments object and turning it
    into an array
  2. ES 6 rest parameters
42
Q

Difference between rest and spread opperator

A

Rest Parameter is collecting all remaining elements into an array . Spread Operator is unpacking collected elements such as arrays into single elements