Advanced Javascript Flashcards

1
Q

What is the main idea behind functional programming?

A

Functional programming aims to bring the precision, consistency, and predictability of mathematical functions into our programming environment. This means striving for “purefunctions that always produce the same output given the same input and do not cause side effects, like altering global state. This emphasis on predictability and consistency can make functional programs easier to test, debug, and understand.

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

What are the three main concepts behind functional programming?

A

The three main concepts behind functional programming are immutability, separation of functions and data, and the use of first-class functions.

Immutability: This is the idea that once a data structure is created, it cannot be changed. Any ‘changes’ to the structure result in a new structure being created, leaving the original untouched. This avoids issues with shared state and reduces bugs, especially in concurrent and parallel programming.

Separation of functions and data: In functional programming, data and functions are kept separate. Functions take in data as arguments and produce new data as output, without modifying the original data.

First-class functions: This means functions in the language are treated as any other variable. They can be assigned to variables, stored in data structures, passed as arguments to other functions, and returned as values from other functions.

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

What is immutability in the context of functional programming?

A

In functional programming, immutability refers to the concept that once a value is assigned to a variable, it cannot be changed. This means that once a variable is defined, its value stays the same throughout the entire program.

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

What are the advantages of using immutability in functional programming?

A

Predictability: With immutability, a variable’s value is constant once assigned, which eliminates concerns about variables’ values changing unpredictably during the execution of a program. This consistency makes the program easier to understand and debug.

Single Source of Truth: An immutable set of data serves as a single source of truth.

Mutation tracking: Immutability facilitates the process of identifying changes in data over time by making use of reference and value equality, which simplifies the identification of changes.

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

In functional programming, how do you manage situations where you need to update a value, given the concept of immutability?

A

In functional programming, when a value needs to be updated, a new variable is defined to represent the updated data, leaving the original data unchanged. This way, immutability is preserved, and you still have access to the original data, making it easier to trace the changes and understand the evolution of the data.

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

What does the separation of data and functions mean in functional programming?

A

The concept of separating data and functions refers to the idea that data (like variables, arrays, and objects) and functions (the operations we can apply to data) exist independently of each other. Functions operate on data by taking it as input and returning a modified copy (respecting immutability) rather than altering the original data in-place.

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

What is the effect of the rule of immutability on functions in functional programming?

A

In functional programming, the rule of immutability states that data cannot be changed once it’s created. This principle directly impacts how functions operate on data. Instead of altering the original data functions in functional programming return a modified copy of the data. This approach respects immutability and prevents side effects (unexpected changes to program state), leading to code that is easier to understand, test, and debug.

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

What is a first-class function?

A

A first-class function is a function that can be treated like any other data type in a programming language. This means that first-class functions can be assigned to variables, stored in data structures, passed as arguments to other functions, and returned as results from other functions.

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

What is the limitation of the const keyword in enforcing immutability in JavaScript?

A

In JavaScript, while the const keyword prevents reassigning a variable to a new value, it does not ensure complete immutability, especially when it comes to arrays and objects. We can still change the individual elements of an array or properties of an object that’s been declared with const.

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

What is ESLint and how can it help ensure immutability in JavaScript?

A

ESLint is a tool for identifying and reporting patterns found in ECMAScript/JavaScript code. It can be used to enforce coding rules and styles. When used with the ESLint-plugin-immutable, it can help in identifying and preventing mutations in JavaScript code, thereby enforcing immutability.

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

What is a closure in JavaScript?

A

A closure in JavaScript is a feature where a function returned from another function retains access to the scope of the function that created and returned it. This means that the returned function can still reference variables from the outer function’s scope, even after the outer function has completed execution.

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

Why are closures useful in JavaScript?

A

In programming, a closure is a combination of a function and the lexical environment in which it was created. It allows a function to remember and access its lexical scope, even when that function is executing outside its lexical scope.

Closures are useful because they enable data encapsulation, provide a way to maintain persistent state within functions, allow for the creation of private variables and functions, and facilitate the use of callback functions by preserving the context and state of operations.

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

What is the single responsibility principle and why is it important?

A

The single responsibility principle is a programming concept that suggests every module, class, or function should have responsibility over a single part of the functionality provided by the software. This principle is important as it makes code more readable, maintainable, and easier to test or debug, as each piece of code has only one job to do.

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

What are higher-order functions in JavaScript?

A

Higher-order functions in JavaScript are functions that can receive other functions as arguments and/or return them as results.

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

What is the reduce function in JavaScript used for ?

A

The reduce function in JavaScript is used to transform an array into a single value, such as the sum, average, or product of its elements.

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

What is partial application in programming and how is it beneficial?

A

Partial application is a technique in programming where a function that takes multiple arguments is transformed into a function with fewer arguments by fixing some of the arguments to certain values. Partial application can be beneficial when we use a function often in our code and some of the arguments rarely change. By fixing those arguments, we can create a more specific function with fewer arguments to manage.

function multiply(x, y, z) {
    return x * y * z;
}

function multiplyByTwoAndThree(z) {
    return multiply(2, 3, z);
}

let result = multiplyByTwoAndThree(4); 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is recursion in programming?

A

Recursion in programming refers to a method where a function calls itself to solve a problem. Each time the function calls itself, it does so with a smaller or simpler version of the problem until it reaches a base condition which stops the recursion.

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

What is ‘recursion depth’ and why is it important?

A

Recursion depth refers to the number of times a recursive function calls itself before reaching the base case. It’s an important consideration in recursion as deep recursion can lead to a stack overflow error, particularly in languages or environments that don’t optimize for recursive calls.

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

What do the call and apply methods of a function in JavaScript do?

A

The call and apply methods in JavaScript are used to invoke a function. The call method takes arguments separately, while the apply method takes arguments as an array. Both methods can also take an initial argument to set the value of ‘this’ within the function

Call example:
~~~
object.objectMethod.call( objectInstance, arguments )
~~~

Apply example:
~~~
object.objectMethod.apply(objectInstance, arrayOfArguments)
~~~

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

What is the class pattern in JavaScript?

A

The class pattern in JavaScript is a design pattern that allows us to define a blueprint for a specific type of object. This blueprint, or class, defines properties and methods common to all objects of this type.

21
Q

What is a constructor in JavaScript classes?

A

A constructor is a special method in a JavaScript class that gets automatically called when a new instance of the class is created. It’s used to initialize the properties of the object.

22
Q

What are JavaScript design patterns and why are they useful?

A

JavaScript design patterns are reusable solutions to common problems in software design. They serve as templates that can help write efficient, maintainable, and scalable code. Design patterns are beneficial as they promote code reuse and efficiency, improve understandability, and help prevent common issues by offering well-proven and tested solutions.

23
Q

What is the Constructor Pattern in JavaScript?

A

The Constructor Pattern in JavaScript involves creating a class and then using that class to create an extended class. The extended class can inherit properties from the parent class and also add new properties.

24
Q

What does the Constructor Pattern allow you to do with classes?

A

The Constructor Pattern allows you to create a class and then create an extended class from it, inheriting properties and methods, and potentially adding more properties or methods. This is useful when you want to create multiple subcategories of a class.

25
Q

What is the super keyword used for in JavaScript?

A

The super keyword in JavaScript is used within the constructor of a subclass to call the constructor of the parent class. It allows access to the parent class’s properties and methods

26
Q

What is the Singleton Pattern in JavaScript?

A

The Singleton Pattern in JavaScript ensures that a class has only one instance, and provides a global point of access to it. When you try to create a new instance of a class that is a singleton, it returns the existing instance.

27
Q

What is the use case for the Singleton Pattern and why is it sometimes useful?

A

The Singleton Pattern is particularly useful when exactly one object is needed to coordinate actions across the system. It provides a way to wrap a unique instance of a class to ensure it is accessed globally and cannot be re-instantiated.

28
Q

What is the Factory Pattern in JavaScript?

A

The Factory Pattern in JavaScript is a design pattern which provides a way to create objects without needing to use the constructor of the class that will be created. It involves a factory method that handles the creation of objects based on the input it receives.

29
Q

What is the Abstract Factory pattern in JavaScript?

A

The Abstract Factory pattern is a creational design pattern that allows you to produce families of related objects without specifying their concrete classes. In JavaScript, an Abstract Factory might be a function that accepts criteria and returns a concrete object from one of several factories.

30
Q

What is a callback function in JavaScript and why is it useful?

A

A callback function in JavaScript is like any other function, but it is designed to be passed as an argument to another function and is executed (“called back”) within that function. This can be used to ensure certain code doesn’t run until other code has finished execution, which is particularly useful in asynchronous operations such as making API calls.

31
Q

What are “Creational design patterns” in programming?

A

Creational design patterns are a set of design patterns in software development that focus on object creation mechanisms. These patterns provide general solutions to create objects in a flexible and reusable way.

Creational design patterns address various object creation scenarios, such as creating objects in a controlled manner, hiding the complexity of object creation, and providing ways to create objects based on specific conditions or configurations.

32
Q

What are “Structural design patterns” in programming?

A

Structural design patterns are concerned with how classes and objects are composed to form larger structures. They provide a way to ensure that changes in one part of a system don’t require changes to the entire system, and help** fit parts** of the system together that might not naturally fit together.

33
Q

What is a module in JavaScript and why do we use them?

A

A module is a function or group of similar functions. They are grouped together within a file and contain the code to execute a specific task when called into a larger application. We use them to better organize and structure the codebase. They’re useful for breaking down large programs into smaller, more manageable, and more independent chunks of code which carry out a single or a couple of related tasks.

34
Q

What is a mixin in JavaScript?

A

In JavaScript, mixins refer to a way of combining the properties and methods from multiple objects into a single object. They allow for the reuse of code enable you to enhance the functionality of an object or class without altering the original class definition. Mixins are not a built-in feature of JavaScript, but rather a programming pattern that can be implemented using various techniques.

35
Q

Why are mixins useful in Javascript?

A

Code reuse: Mixins allow you to define reusable behavior that can be easily applied to multiple objects instead of duplicating code across different objects.

Flexibility: Mixins enable you to compose objects with different sets of behaviors by selecting and combining relevant mixins. This provides a flexible way to customize and extend objects without the need for complex inheritance hierarchies.

Avoiding inheritance limitations: JavaScript has single inheritance, meaning an object can inherit from only one prototype. Mixins provide a way to overcome this limitation by allowing objects to inherit from multiple sources.

36
Q

How do you apply a mixin to an existing object or class in JavaScript?

A

A mixin can be applied to an existing object or class in JavaScript using the Object.assign() method. This method takes the **target object **(or class prototype) and the source object (the mixin) as arguments and copies the properties from the source to the target.

37
Q

What is a facade pattern in programming and why is useful?

A

The facade pattern in programming is a structural design pattern that provides a simplified interface to a complex system. It hides the complexities of the larger system and provides a simpler interface to the client. It’s useful because it helps to reduce complexity and improve readability of code.

38
Q

What is the flyweight pattern in programming?

A

The flyweight pattern in programming is a design pattern used to minimize memory use by sharing data across similar objects. It prevents unnecessary duplication of data in memory.

39
Q

How is the flyweight pattern both similar and dissimilar to the Singleton pattern?

A

The flyweight pattern is similar to the Singleton pattern in that they both aim to reduce unnecessary duplication. However, while the Singleton pattern ensures there is only one instance of a class, the flyweight pattern focuses on sharing data between instances to reduce memory usage.

40
Q

What is the Model-View-Controller (MVC) pattern in software design and what are the different parts responsible for?

A

The MVC pattern is a design pattern that separates an application into three main logical components: the model, the view, and the controller. This pattern is used to isolate the concerns of data (model), user interface (view), and control flow (controller) to promote organization and scalability.

The model is responsible for managing the data of the application. The view is responsible for presenting the data to the user, often as a user interface. The controller handles user input, interacts with the model to fetch or update data, and updates the view accordingly.

41
Q

What is the Model-View-ViewModel (MVVM) pattern and where is it commonly used?

A

The MVVM pattern is a design pattern that separates an application into three components: the View, the ViewModel, and the Model. The View represents the user interface, the ViewModel manages the logic and the state of the data, and the Model interacts with the back-end for data processing.

The MVVM pattern is commonly used in applications developed with frameworks like React and Angular. In React, for instance, stateless components represent the View, stateful components act as the ViewModel, and the Model is where React connects to the back-end to process data.

42
Q

What is the Observer pattern and where is it commonly used?

A

The Observer pattern is a behavioural design pattern
that offers a subscription model in which objects subscribe to an event and get notified when the event occurs. This pattern is the most commonly used in event driven systems.

43
Q

What is the State pattern?

A

The State pattern is a behavioural design pattern that allows an object to change its behavior when its internal state changes. In web development, this often means re-rendering parts of the application when the data they depend on changes.

44
Q

What is the ‘base case’ in recursion?

A

The base case in recursion is the condition that stops the function from calling itself endlessly. It defines the point where the function should stop making recursive calls and start returning results.

45
Q

What is a stack overflow and how can you prevent it?

A

A stack overflow occurs when a program uses more memory than the call stack has available, usually because of infinite recursion. When a stack overflow happens, the program can’t create new instances of functions, and this may cause the program and the computer to crash.

To prevent stack overflow in recursive functions, you need to ensure that each recursive call changes the state of the program in a way that moves closer to the base case. The base case must eventually be reached to stop further recursive calls.

46
Q

How is tail recursion different from head recursion and which one is more efficient?

A

In tail recursion, the processing happens before the recursive call. In head recursion, the recursive call happens first, and the processing happens after.

Tail recursion is more efficient because the computer can forget about the earlier steps once they are done. In head recursion, the computer has to remember all the steps, which can use up a lot of memory.

47
Q

Define “memoization” in the context of computer programming.

A

Memoization is an optimization technique used primarily to speed up programs by storing the results of expensive function calls and reusing them when the same inputs are used again. It is a form of caching where computed results are stored for future use.

48
Q

Can you detail the process of how memoization works?

A

Memoization works by maintaining a map or an array (a cache) to keep track of previously computed results of a function call. When a function is called, the memoization procedure checks the cache to see if the result for the given input is already available. If it is, the cached result is returned, bypassing the execution of the function’s logic. If the result isn’t in the cache, the function is executed, the result is cached, and then returned. This technique optimizes the program execution by avoiding unnecessary recomputation.