Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
To get a job as a front end developer, we need to nail the coding interview.
In this article, we’ll look at some theoretical questions about JavaScript
Name two programming paradigms important for JavaScript app developers.
JavaScript supports both object-oriented programming (OOP) with some functional programming concepts added in.
JavaScript supports OOP via prototypical inheritance.
The JavaScript class syntax is just syntactic sugar on top of its prototypical inheritance model.
Functional programming concepts that are part of JavaScript include closures, first-class functions, and anonymous functions.
Where is functional programming concepts used in JavaScript?
Functional programming concepts include pure functions, which are functions that avoid side effects, composing functions, and passing functions as arguments since functions are first class.
The immutability of objects are also important with functional programming to avoid bugs from multiple entities changing the same object.
What is the difference between classical inheritance and prototypal inheritance?
Language with class inheritance has classes that are blueprints for creating objects.
We can also have subclasses that are children of a parent class. Therefore, there is a class hierarchy.
Instances of the class are created with the new
operator.
Prototypical inheritance is where instances of an object directly inherit from a prototype object.
Instances are generally created from factory functions. Instances may be composed of many different objects.
JavaScript uses prototypical inheritance despite the use of the new
operator.
The new
operator is used with constructor functions, which are factory functions that are used to create new instances of objects.
The class syntax is the same as a constructor function. JavaScript doesn’t use class-based inheritance despite the use of the class syntax.
JavaScript classes are still constructor functions with a cleaner, more easily understood syntax.
What are the pros and cons of functional programming vs object-oriented programming?
OOP is easy to understand and the basic concepts of objects are easy.
Therefore, it’s easy to interpret to the meaning of its members.
OOP uses an imperative style of programming rather than a declarative style. Imperative programming means that statements are used to change a program’s state.
Declarative programming describes logic without describing the control flow.
OOP depends on shared state. Objects and behaviors are put together into one entity. The members of the entity may be accessed in any more.
This may lead to issues like race conditions.
Functional programming uses pure functions to avoid any side effects. This eliminates bugs from multiple functions changing the same resource.
Functions tend to be simplified and composable for more reusable code compared to OOP.
Functional programming generally uses declarative styles, which do not write out everything step by step.
This means there’s more room for refactoring and we can replace algorithms with more efficient ones more easily.
Because of its declarative style, functional programming code is harder to read since not everything is written out line-by-line.
Functional programming also has more academic material than OOP materials since OOP has more materials targeted at beginners because of its popularity.
When is classical inheritance an appropriate choice?
Classic inheritance is only appropriate for a single level of inheritance. Multiple levels of inheritance is always an antipattern.
Multiple levels are confusing and hard to read.
Object composition is better than inheritance.
When is prototypal inheritance an appropriate choice?
Prototypical inheritance is useful since it allows for composition.
This means that objects can have the has-a, uses-a or can-do relationship, as opposed to the is-a relationship with class inheritance.
We can compose objects from multiple sources with prototypical inheritance and use mixins or Object.assign
to combine multiple objects into one.
What does “favor object composition over class inheritance” mean?
Code should be assembled from smaller units of functionality into new objects instead of inheriting from classes and creating object taxonomies.
This means that instead of an is-a relationship, they can have a can-do, has-a, or uses-a relationships.
With composition, we avoid class hierarchies, tight coupling, and rigid taxonomy.
It also makes code more flexible.
What is asynchronous programming, and why is it important in JavaScript?
JavaScript has a single-threaded runtime environment. That means that all synchronous code will block the main execution thread.
Long-running networking requests and I/O operations will prevent the rest of the app from running.
Asynchronous programming means that the engine runs in an event loop. Blocking operations are queued in the event loop instead of running immediately. Therefore, the code won’t block the rest of the code from running.
When the response is ready, then the event handler runs and the control flow for the asynchronous code continues.
This way, a single program thread can handle many concurrent operations.
Browser JavaScript environment and Node.js are both async by default.
Conclusion
It’s important to know that JavaScript is both an object-oriented and functional language.
Objects are composed of inheriting from prototype objects instead of having a rigid class hierarchy. The class syntax doesn’t make that any different.
Objects are created from constructor functions.
JavaScript is a single-threaded language, so concurrent operations have to be run asynchronously to preventing blocking the main thread.