JavaScript Interview Questions - 1

  1. Name 2 programming paradigms important for JavaScript app developers

  1. What is Functional Programming?
    • Pure function / function purity;
    • Avoid side-effects;
    • Simple function composition;
    • Features that support Functional Programming: first-class functions, higher order functions, functions as arguments/values;
    • Examples of functional languages: Lisp, ML, Haskell, Erlang, Clojure, Elm, F Sharp, OCaml, etc;

  1. Difference between Classical inheritance and Prototypal Inheritance?
    • Classical: Create tight coupling or hierarchies/taxonomies;
    • Prototypes: mentions of concatenative inheritance, prototype delegation, functional inheritance, object composition.
    • Class Inheritance: instances inherit from classes (like a blueprint — a description of the class), and create sub-class relationships: hierarchical class taxonomies. Instances are typically instantiated via constructor functions with the new keyword. Class inheritance may or may not use the class keyword from ES6.
    • Prototypal Inheritance: instances inherit directly from other objects. Instances are typically instantiated via factory functions or Object.create(). Instances may be composed from many different objects, allowing for easy selective inheritance.

  1. What are the pros and cons of Functional Programming vs Object-Oriented Programming
    • OOP Pros: It's easy to understand the basic concept of objects and easy to interpret the meaning of method calls. OOP tends to use an imperative style rather than a declarative style, which reads like a straight-forward set of instructions for the computer to follow.
    • OOP Cons: OOP Typically depends on the shared state. Objects and behaviors are typically tacked together on the same entity, which may be accessed at random by any number of functions with non-deterministic order, which may lead to undesirable behavior such as race conditions.
    • Functional Programming Pros: Using the functional paradigm, programmers avoid any shared state or side-effects, which eliminates bugs caused by multiple functions competing for the same resources. With features such as the availability of point-free style (aka tacit programming), functions tend to be radically simplified and easily recomposed for more generally reusable code compared to OOP. Functional Programming also tends to favor declarative and denotational styles, which do not spell out step-by-step instructions for operations, but instead concentrate on what to do, letting the underlying functions take care of the how. This leaves tremendous latitude for refactoring and performance optimization, even allowing you to replace entire algorithms with more efficient ones with very little code change. (eg., memorize, or user lazy evaluation in place of eager evaluation.) The computation that makes use of pure functions is also easy to scale across multiple processors, or across distributed computing clusters without fear of threading resource conflicts, race conditions, etc...
    • Functional Programming Cons: Over exploitation of Functional Programming features such as point-free style and large compositions can potentially reduce readability because the resulting code is often more abstractly specified, more terse, and less concrete. Functional Programming has a much steeper learning curve than OOP because the broad popularity of OOP has allowed the language and learning materials of OOP to become more conversational, whereas the language of Functional Programming tends to be much more academic and formal. Functional Programming concepts are frequently written about using idioms and notations from lambda calculus, algebras, and category theory, all of which requires a prior knowledge foundation in those domains to be understood.
    • Summary:
      - The trouble with the shared state, different things competing for the same resources, etc...
      - Awareness of Function Programming's capability to radically simplify many applications.
      - Awareness of the differences in learning curves.
      - Articulation of the side-effects and how they impact program maintainability.
      - Awareness that a highly functional codebase can have a steep learning curve.
      - Awareness that a highly OOP codebase can be extremely resistant to change and very brittle compared to an equivalent Functional Programming codebase.
      - Awareness that immutability gives rise to an extremely accessible and malleable program state history, allowing for the easy addition of features like infinite undo/redo, rewind/replay, time-travel debugging, and so on. Immutability can be achieved in either paradigm, but a proliferation of shared stateful objects complicates the implementation in OOP.

  1. When is classical inheritance an appropriate choice?
    • Rarely, almost never, or never.
    • A single level is sometimes OK, from a framework base-class such as React.Component.

  1. When is prototypal inheritance an appropriate choice?
    • Delegation (i.e., the prototype chain)
    • Concatenative (i.e. mixins, Object.assign())
    • Functional (Not to be confused with functional programming. A function used to create a closure for private state/encapsulation)
    • Bouns:
      - In situations where modules or functional programming don’t provide an obvious solution.
      - When you need to compose objects from multiple sources.
      - Any time you need inheritance.

  1. What does “favor object composition over class inheritance” mean?
    • Avoid class hierarchies.
    • Avoid brittle base class problem.
    • Avoid tight coupling.
    • Avoid rigid taxonomy (forced is-a relationships that are eventually wrong for new use cases).
    • Avoid the gorilla banana problem (“what you wanted was a banana, what you got was a gorilla holding the banana, and the entire jungle”).
    • Make code more flexible.

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

  1. What are the pros and cons of monolithic vs microservice architectures?

  1. What is asynchronous programming, and why is it important in JavaScript?
    • Synchronous programming means that, barring conditionals and function calls, the code is executed sequentially from top-to-bottom, blocking on 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 needed, the request is started, and the code keeps running without blocking for the result. When the response is ready, an interrupt is fired, which causes an event handler to be run, where the control flow continues. In this way, a single program thread can handle many concurrent operations.
    • User interfaces are asynchronous by nature and spend most of their time waiting for user input to interrupt the event loop and trigger event handlers.
    • Node.js is asynchronous by default, meaning that the server works in much the same way, waiting in a loop for a network request, and accepting more incoming requests while the first one is being handled.
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,499评论 0 10
  • 今天收获: 1.朋友圈看到晓燕写的“请相信,这个世界上真的有人在过着你想要的生活。也请明白,那些人大都曾隐忍过你尚...
    莹安阅读 201评论 0 0
  • 01 最近在参加韩大爷的领读班,由于工作较忙,很多时候需要后期自己补课,比如像今天周六,开始补本周读王小波文章所落...
    Sunny格桑花阅读 762评论 0 1
  • 女生每次都来游戏厅,在她心情不好的时候,有时候是刚刚工作回来,有时更像是个习惯。 她玩的是夹娃娃机,她的技术一般般...
    番茄也行阅读 591评论 0 0
  • 作为一枚程序员,一枚好久没有双休过的程序员,我的回答是: 一个月才过一个周末不算有,几个月偶尔加一次班才算。加班,...
    缝雨阅读 2,904评论 0 0