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.
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,923评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,154评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,775评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,960评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,976评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,972评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,893评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,709评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,159评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,400评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,552评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,265评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,876评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,528评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,701评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,552评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,451评论 2 352

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,322评论 0 10
  • 今天收获: 1.朋友圈看到晓燕写的“请相信,这个世界上真的有人在过着你想要的生活。也请明白,那些人大都曾隐忍过你尚...
    莹安阅读 183评论 0 0
  • 01 最近在参加韩大爷的领读班,由于工作较忙,很多时候需要后期自己补课,比如像今天周六,开始补本周读王小波文章所落...
    Sunny格桑花阅读 740评论 0 1
  • 第40本书。5.24日阅读完雪球徐大为的《寻找实现30%年收益率的奥秘-低风险投资之路-低风险也能高收益》这本书。...
    swanlin阅读 621评论 0 2
  • 女生每次都来游戏厅,在她心情不好的时候,有时候是刚刚工作回来,有时更像是个习惯。 她玩的是夹娃娃机,她的技术一般般...
    番茄也行阅读 540评论 0 0