10 Interview Questions Every JavaScript Programmer Should Know

Brushing up questions is a very important thing in programmer interview preparation. It directly determines whether you can succeed in the interview, or whether you have an active advantage in salary negotiations.

JavaScript is special, it plays a vital role in almost every large application. If you're a JavaScript programmer, here are some questions to help you explore what's really important.

What programming paradigms are important to JavaScript application developers?

JavaScript is a multi-paradigm language that supports imperative, procedural programming as well as OOP (object-oriented programming) and functional programming. JavaScript supports OOP with prototypal inheritance.

What is functional programming?

Functional programming generates programs by combining mathematical functions and avoids shared state and mutable data. Lisp (specified in 1958) was one of the first languages ​​to support functional programming, and was heavily inspired by the lambda calculus. Lisp and many Lisp-family languages ​​are still widely used today.

Functional programming is a programming paradigm based on functions, where functions are treated as first-class citizens. Functions can be passed as arguments to other functions and returned as return values. Functional programming encourages the use of immutable data and side-effect-free functions. This means that the function only depends on its inputs and does not change any external state. In JavaScript, functional programming can be implemented using concepts such as higher-order functions, closures, and recursion.

What is the difference between classical inheritance and prototypal inheritance?

Class Inheritance: Instances inherit from classes (like blueprints - descriptions of classes), and create subclass relationships: Hierarchical Class Taxonomy. Instances are usually instantiated via a constructor with the "new" keyword. Class inheritance may or may not use keywords in ES6 class.

Prototypal Inheritance: Instances inherit directly from other objects. Instances are usually instantiated via factory functions or Object.create(). An instance can be composed of many different objects, allowing easy selective inheritance.

In JavaScript, prototypal inheritance is simpler and more flexible than class inheritance.

What are the pros and cons of functional programming vs object oriented programming?

OOP advantages: easy to understand the basic concepts of objects, easy to understand the meaning of method calls. OOP tends to use an imperative rather than a declarative style, which reads like a straightforward set of instructions for a computer to follow.

OOP Cons: OOP typically relies on shared state. Objects and behavior are often bundled together on the same entity, which can be randomly accessed by any number of functions with indeterminate order, which can lead to undesirable behavior such as race conditions.

Advantages of FP: Using the functional paradigm, programmers can avoid any shared state or side effects, thereby eliminating bugs caused by multiple functions competing for the same resources. Compared to OOP, with features such as point-free style (aka default programming), functionality tends to be radically simplified and easily reorganized into more generally reusable code.

FP also tends to favor declarative and denotative styles, which don't specify step-by-step instructions for operations, but instead focus on what to do and let the underlying functions take care of how to do it. This leaves huge room for refactoring and performance optimization, and even allows you to replace an entire algorithm with a more efficient one with very few code changes. (For example, memoize or use lazy evaluation instead of eager evaluation.)
Computations using pure functions are also easily scalable across multiple processors or across distributed computing clusters without worrying about thread resource conflicts, race conditions, etc...

FP Cons: Excessive use of FP features such as dot-free styles and large combinations can reduce readability because the generated code is often more abstractly specified, more concise, and less specific.
More people are familiar with OO and imperative programming than functional programming, so even common idioms in functional programming can confuse new team members.

FP has a much steeper learning curve than OOP because the widespread popularity of OOP has made OOP's language and learning materials more conversational, while FP's language tends to be more academic and formal. FP concepts are often written about using idioms and notations from lambda calculus, algebra, and category theory, all of which require a prior knowledge base in these areas to understand.

When is classical inheritance an appropriate choice?

The answer is never, or almost never. Of course never more than one level. Multi-level class hierarchies are an anti-pattern.

When is prototypal inheritance an appropriate choice?

There is more than one type of prototypal inheritance:

  • Delegation (i.e. the prototype chain).
  • concatenation (i.e. mixins, Object.assign()).
  • Functional (not to be confused with functional programming. Functions for creating closures for private state/encapsulation).

Each type of prototypal inheritance has its own set of use cases, but they are equally useful in enabling composition, which creates has-a or uses-a or can-do relationships, rather than is-a relationships created using class inheritance.

What does "object composition takes precedence over class inheritance" mean?

This means that code reuse should be achieved by assembling smaller functional units into new objects, rather than inheriting from classes and creating object taxonomies.
In other words, use can-do, has-a, or uses-a relationships, not is-a relationships.

What are two-way data binding and one-way data flow, and how are they different?

Two-way data binding means that UI fields are dynamically bound to model data so that when the UI fields change, the model data changes, and vice versa.

One way of data flow means that the model is the only source of truth. Changes in the UI trigger messages that signal user intent to the model (or "store" in React). Only the model has permission to change the state of the application. The effect is that data always flows in one direction, which is easier to understand.

One way data flow is deterministic, and two-way binding can cause side effects that are hard to read and understand.

What are the advantages and disadvantages of monolithic architecture and microservice architecture?

A monolithic architecture means that your application is written as a cohesive unit of code whose components are designed to work together, sharing the same memory space and resources.
A microservices architecture means that your application consists of many smaller, independent applications that are able to run in their own memory space and scale independently of each other on potentially many independent machines.

Monolithic advantages: The main advantage of monolithic architectures is that most applications typically have a large number of cross-cutting concerns, such as logging, rate limiting, and security features such as audit trails and DOS protection.

When everything runs through the same application, it's easy to connect components to those cross-cutting concerns.

There may also be a performance benefit, since shared memory access is faster than interprocess communication (IPC).

Monolithic disadvantages: Monolithic application services tend to be tightly coupled and entangled as the application evolves, making it difficult to isolate services for purposes such as independent scaling or code maintainability.

Monoliths are also harder to understand because there can be dependencies, side effects, and magic that aren't obvious when you're looking at a specific service or controller.

Microservices Pros: Microservices architectures are generally better organized because each microservice has a very specific job and doesn't care about the jobs of other components. Separated services are also easier to reorganize and reconfigure to serve different application purposes (for example, serving web clients and public APIs at the same time).

They can also have performance benefits, depending on how they are organized, because hot services can be isolated and scaled independently of the rest of the application.

Microservices Cons: When building a new microservices architecture, you may discover many cross-cutting concerns that were not anticipated at design time. A single application can build shared magic helpers or middleware to handle such cross-cutting problems without much effort.
In a microservices architecture, you need to incur the overhead of a separate module for each cross-cutting concern, or encapsulate the cross-cutting concerns in another service layer through which all traffic passes.

Ultimately, even monoliths tend to route traffic through external service layers for cross-cutting concerns, but with a monolith, the cost of this effort can be delayed until the project is more mature.

Microservices are often deployed on their own virtual machines or containers, leading to a surge in VM wrangling work. These tasks are often automated through container fleet management tools.

What is asynchronous programming, and why is it important in JavaScript?

Synchronous programming means that, except for conditions and function calls, code is executed sequentially from top to bottom, blocking long-running tasks such as network requests and disk I/O.

Asynchronous programming means that the engine runs in an event loop. When a blocking operation is required, the request is initiated and the code continues to run without blocking on the result. When the response is ready, an interrupt fires, which causes an event handler to run, at which point control flow continues. In this way, a single program thread can handle many concurrent operations.

User interfaces are asynchronous in nature, spending most of their time waiting for user input to interrupt the event loop and trigger event handlers.

By default, Node is asynchronous, which means that the server works in much the same way, waiting in a loop for network requests, and accepting more incoming requests while the first one is being processed.

This is important in JavaScript because it's great for user interface code, and it's great for server performance.

Original: https://medium.com/javascript-scene/10-interview-questions-every-javascript-developer-should-know-6fa6bdf5ad95, this article is published after translation

Guess you like

Origin blog.csdn.net/Mr_HelloWorldx/article/details/131375109