javascript面試_在編碼面試中需要注意的3個JavaScript問題

javascript面試

JavaScript is the official language of all modern web browsers. As such, JavaScript questions come up in all sorts of developer interviews.

JavaScript是所有現代Web瀏覽器的官方語言。 因此,各種開發人員訪談中都會出現JavaScript問題。

This article isn’t about the newest JavaScript libraries, common development practices, or any of the new ES6 functions. Rather, it’s about 3 things that usually come up in interviews when discussing JavaScript. I myself have been asked these questions, and my friends have told me they’ve been asked them, too.

本文不是關于最新JavaScript庫,常見的開發實踐或任何新的ES6函數 。 相反,在討論JavaScript時,通常在采訪中會涉及到三件事。 我自己也被問過這些問題,我的朋友們也告訴我他們也被問過這些問題。

Of course these aren’t the only 3 things you should study before a JavaScript interview — there are a multitude of ways you can better prepare for an upcoming interview — but below are 3 questions an interviewer may ask to judge how well you know and understand the JavaScript language and the DOM.

當然,這些并不是您在JavaScript面試之前應該學習的唯一三件事-有多種 方法 可以更好地為即將到來的面試做準備-但是以下是面試官可能要問的三個問題,以判斷您對自己的了解和了解的程度JavaScript語言和DOM 。

So let’s get started! Note that we’re going to use vanilla JavaScript in the examples below, since your interviewer will usually want to see how well you understand JavaScript and the DOM without the help of libraries like jQuery.

因此,讓我們開始吧! 請注意,在下面的示例中,我們將使用原始JavaScript,因為您的訪問員通常會希望在沒有jQuery之類的幫助的情況下,了解您對JavaScript和DOM的理解程度。

問題1:事件委托 (Question 1: Event delegation)

When building an application, sometimes you’ll need to attach event listeners to buttons, text, or images on the page in order to perform some action when the user interacts with the element.

在構建應用程序時,有時您需要將事件偵聽器附加到頁面上的按鈕,文本或圖像上,以便在用戶與元素進行交互時執行某些操作。

If we take a simple todo list as an example, the interviewer may tell you that they want an action to occur when a user clicks one of the list items. And they want you to implement this functionality in JavaScript assuming the following HTML code:

如果我們以一個簡單的待辦事項列表為例,訪問者可能會告訴您,當用戶單擊列表項之一時,他們希望采取某種措施。 他們希望您假設以下HTML代碼在JavaScript中實現此功能:

You may want to do something like the following to attach event listeners to the elements:

您可能需要執行以下操作,將事件偵聽器附加到元素:

While this does technically work, the problem is that you’re attaching an event listener to every single item individually. This is fine for 4 elements, but what if someone adds 10,000 items (they may have a lot of things to do) to their todo list? Then your function will create 10,000 separate event listeners and attach each of them to the DOM. This isn’t very efficient.

盡管這在技術上是可行的,但問題是您將事件偵聽器分別附加到每個項目。 這對于4個元素來說很好,但是如果有人將10,000個項目(他們可能有很多事情要做)添加到他們的待辦事項列表中怎么辦? 然后,您的函數將創建10,000個獨立的事件偵聽器,并將每個偵聽器都附加到DOM。 這不是很有效 。

In an interview it would be best to first ask the interviewer what the maximum number of elements the user can enter is. If it can never be more than 10, for example, then the above code would work fine. But if there’s no limit to the number of items the user can enter, then you’d want to use a more efficient solution.

在采訪中,最好先問問采訪者,用戶可以輸入的最大元素數是多少。 例如,如果它不能超過10,那么上面的代碼將可以正常工作。 但是,如果用戶可以輸入的項目數沒有限制,那么您想使用更有效的解決方案。

If your application could end up with hundreds of event listeners, the more efficient solution would be to actually attach one event listener to the whole container, and then be able to access each item when it’s actually clicked. This is called event delegation, and it’s much more efficient than attaching separate event handlers.

如果您的應用程序最終可能包含數百個事件偵聽器,則更有效的解決方案是將一個事件偵聽器實際附加到整個容器,然后在實際單擊每個項目時便能夠訪問它們。 這稱為事件委托 ,它比附加單獨的事件處理程序效率更高。

Here’s the code for event delegation:

這是事件委托的代碼:

問題2:在循環中使用閉包 (Question 2: Using a closure within a loop)

Closures are sometimes brought up in an interview so that the interviewer can gauge how familiar you are with the language, and whether you know when to implement a closure.

有時會在面試中提出封閉方式,以便訪問員可以評估您對語言的熟悉程度以及是否知道何時實施封閉。

A closure is basically when an inner function has access to variables outside of its scope. Closures can be used for things like implementing privacy and creating function factories. A common interview question regarding the use of closures is something like this:

閉包基本上是指內部函數可以訪問其范圍之外的變量。 閉包可以用于實現隱私和創建功能工廠之類的事情。 有關使用閉包的常見采訪問題是這樣的:

Write a function that will loop through a list of integers and print the index of each element after a 3 second delay.

編寫一個將遍歷整數列表并在3秒的延遲后打印每個元素的索引的函數。

A common (incorrect) implementation I’ve seen for this problem looks something like this:

我為這個問題所見的常見(錯誤)實現看起來像這樣:

If you run this you’ll see that you actually get the 4 printed out every time instead of the expected 0, 1, 2, 3 after a 3 second delay.

如果運行此命令,則會看到您實際上每次都打印了4個 ,而不是在3秒鐘的延遲后輸出了預期的0、1、2、3

To correctly identify why this is happening it would be useful to have an understanding of why this happens in JavaScript, which is exactly what the interviewer is trying to test.

要正確確定這種情況的發生原因,有必要了解JavaScript中這種情況發生的原因,這正是訪問員試圖測試的原因。

The reason for this is because the setTimeout function creates a function (the closure) that has access to its outer scope, which is the loop that contains the index i. After 3 seconds go by, the function is executed and it prints out the value of i, which at the end of the loop is at 4 because it cycles through 0, 1, 2, 3, 4 and the loop finally stops at 4.

其原因是因為setTimeout函數創建了一個可以訪問其外部作用域的函數(閉包),該外部作用域是包含索引i的循環。 經過3秒鐘后,該函數將執行并打印出i的值,該值在循環結束時為4,因為它在0、1、2、3、4之間循環,最終循環在4處停止。

There are actually a few ways of correctly writing the function for this problem. Here are two of them:

實際上, 有幾種方法可以正確編寫此問題的函數。 這是其中兩個:

問題3:反跳 (Question 3: Debouncing)

There are some browser events that can fire many times within a short timespan very quickly, such as resizing a window or scrolling down a page. If you attach an event listener to the window scroll event for example, and the user continuously scrolls down the page very quickly, your event may fire thousands of times within the span of 3 seconds. This can cause some serious performance issues.

有些瀏覽器事件可以在很短的時間內Swift觸發多次,例如調整窗口大小或向下滾動頁面。 例如,如果將事件偵聽器附加到窗口滾動事件,并且用戶連續快速向下滾動頁面,則您的事件可能會在3秒的跨度內觸發數千次。 這可能會導致一些嚴重的性能問題。

If you’re discussing building an application in an interview, and events like scrolling, window resizing, or key pressing come up, make sure to mention debouncing and/or throttling as a way to improve page speed and performance. A real example taken from this guest post on css-tricks:

如果您在面試中討論如何構建應用程序,并且出現諸如滾動,窗口大小調整或按鍵之類的事件,請確保提及反跳和/或調節,以提高頁面速度和性能。 一個真實的例子來自于這個關于css-tricks的來賓帖子 :

In 2011, an issue popped up on the Twitter website: when you were scrolling down your Twitter feed, it became slow and unresponsive. John Resig published a blog post about the problem where it was explained how bad of an idea it is to directly attach expensive functions to the scroll event.

2011年,Twitter網站上彈出一個問題:當您向下滾動Twitter feed時,它變得緩慢且無響應。 John Resig發表了一篇關于該問題的博客文章,其中解釋了直接將昂貴的函數附加到scroll事件的想法多么糟糕。

Debouncing is one way to solve this issue by limiting the time that needs to pass by until a function is called again. A correct implementation of debouncing would therefore group several function calls into one and execute it only once after some time has elapsed. Here’s an implementation in plain JavaScript that makes use of topics such as scope, closures, this, and timing events:

消除反跳是解決此問題的一種方法,方法是限制直到再次調用函數之前需要經過的時間。 正確實現的去抖動會因此幾個函數調用成一個,并執行它只有一次經過一段時間之后。 這是純JavaScript的實現,該實現利用了諸如scope ,closures, this和Timing事件之類的主題 :

This function — when wrapped around an event — will execute only after a certain amount of time has elapsed.

該功能(圍繞事件)僅在經過一定時間后才會執行。

You would use this function like so:

您將像這樣使用此功能:

Throttling is another technique that’s is similar to debouncing, except that instead of waiting for some time to pass by before calling a function, throttling just spreads the function calls across a longer time interval. So if an event occurs 10 times within 100 milliseconds, throttling could spread out each of the function calls to be executed once every 2 seconds instead of all firing within 100 milliseconds.

節流是另一種類似于反跳的技術,除了節流只是將函數調用擴展到更長的時間間隔之外,而不是在調用函數之前等待一段時間。 因此,如果一個事件在100毫秒內發生10次,則節流可能會擴展每個要每2秒執行一次的函數調用,而不是在100毫秒內觸發所有事件。

For more information on debouncing and throttling, the following articles and tutorials may be helpful:

有關反跳和限制的更多信息,以下文章和教程可能會有所幫助:

  • Throttling and Debouncing in JavaScript

    JavaScript中的節流和反跳

  • The Difference Between Throttling and Debouncing

    節流與反跳的區別

  • Examples of Throttling and Debouncing

    節流和反跳的示例

  • Remy Sharp’s blog post on Throttling function calls

    雷米·夏普(Remy Sharp)關于節流功能調用的博客文章

If you enjoyed reading this article, then you may like reading the JavaScript tutorials and solving some of the JavaScript coding challenges that I host on Coderbyte. I’d love to hear what you think!

如果您喜歡閱讀本文,那么您可能喜歡閱讀JavaScript教程并解決我在Coderbyte上托管的一些JavaScript編碼難題。 我很想聽聽您的想法!

翻譯自: https://www.freecodecamp.org/news/3-questions-to-watch-out-for-in-a-javascript-interview-725012834ccb/

javascript面試

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395794.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395794.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395794.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【學習筆記】深入理解js原型和閉包(11)——執行上下文棧

繼續上文的內容。 執行全局代碼時,會產生一個執行上下文環境,每次調用函數都又會產生執行上下文環境。當函數調用完成時,這個上下文環境以及其中的數據都會被消除,再重新回到全局上下文環境。處于活動狀態的執行上下文環境只有一個…

Java基礎--訪問權限控制符

今天我們來探討一下訪問權限控制符。 使用場景一:攻城獅A編寫了ClassA,但是他不想所有的攻城獅都可以使用該類,應該怎么辦? 使用場景二:攻城獅A編寫了ClassA,里面有func1方法和func2方法,但是他…

css繪制正方體_設計師僅使用CSS繪制了8個標志性X戰警

css繪制正方體Here are three links worth your time:這是三個值得您花費時間的鏈接: A designer drew 8 iconic X-Men using nothing but CSS (1 minute interactive) 一位設計師僅用CSS繪制了8個標志性的X戰警( 互動時間為1分鐘 ) Raspberry Pi just turned 5. H…

Dubbo簡單介紹及實例

1、概念 Dubbo是一個分布式服務框架,以及阿里巴巴內部的SOA服務化治理方案的核心框架。其功能主要包含:高性能NIO通訊及多協議集成。服務動態尋址與路由。軟負載均衡與容錯,依賴分析與降級等。 說通俗點,就是首先將程序組件化成一…

Oracle 10.2.0.5升級至11.2.0.4

參照MOS 官方文檔Complete Checklist for Manual Upgrade to Oracle Database 11gR2 (11.2) (Doc ID 837570.1)一、升級前的準備1、復制utlu112i.sql腳本從11G數據庫復制$ORACLE_HOME/rdbms/admin/utlu112i.sql 腳本至10g 數據庫臨時目錄,準備執行如果不在10g數據庫…

脫殼_詳細_使用的方法_01

ZC: 如何確定被調試程序已經來到了 未加殼的程序中? ZC:  視頻中是使用判斷集中語言的特征 ZC:  我的方法:上面的方式 ESP平衡 1、第1課 (1)、單步跟蹤(原則:向下的跳轉>正常F8,向上的跳轉>F4跳過(或者用F2…

android 函數式編程_Android開發人員的函數式編程-第1部分

android 函數式編程by Anup Cowkur通過安納普考庫(Anup Cowkur) Android開發人員的函數式編程-第1部分 (Functional Programming for Android Developers — Part 1) Lately, I’ve been spending a lot of time learning Elixir, an awesome functional programming language…

java編程 內存_Java編程技術之淺析JVM內存

JVMJVM->Java Virtual Machine:Java虛擬機,是一種用于計算設備的規范,它是一個虛構出來的計算機,是通過在實際的計算機上仿真模擬各種計算機功能來實現的。基本認知:1.JVM是用于運行Java代碼的假象計算機,主要有一套字節碼指令…

bzoj1116: [POI2008]CLO

傳送門:http://www.lydsy.com/JudgeOnline/problem.php?id1116 題目大意:Byteotia城市有n個 towns m條雙向roads. 每條 road 連接 兩個不同的 towns ,沒有重復的road. 你要把其中一些road變成單向邊使得:每個town都有且只有一個入度 題解&am…

java排序算法大全_各種排序算法的分析及java實現

排序一直以來都是讓我很頭疼的事,以前上《數據結構》打醬油去了,整個學期下來才勉強能寫出個冒泡排序。由于要找工作了,也知道排序算法的重要性(據說是面試必問的知識點),所以又花了點時間重新研究了一下。排序大的分類可以分為兩…

Cocos2d-x 3.0 簡捷的物理引擎

Cocos2d-x 3.0 開發(九)使用Physicals取代Box2D和chipmunk http://www.cocos2d-x.org/docs/manual/framework/native/physics/physics-integration/zh -- 官網Demo 水墨魚的專欄 http://www.cocos2d-x.org/docs/catalog/zh --- 官方 搭“server” 須要哪…

google i/o_Google I / O 2017最有希望的突破

google i/oby Aravind Putrevu通過Aravind Putrevu Google I / O 2017最有希望的突破 (The most promising breakthroughs from Google I/O 2017) Google I/O is one of the biggest developer conferences. This year was particularly exciting. There were two keynotes: o…

java clex 中的 IloLPMatrix

最近看 cplex 在 java 的 callback,發現它給的 callback 例子中,都是用 IloLPMatrix 這個類放約束條件,在 IloLPMatrix 中, 每個約束條件存儲在 IloRange 中。 使用 IloLPMatrix 的好處是,這個類可以方便查看模型中的求…

6/12 Sprint2 看板和燃盡圖

轉載于:https://www.cnblogs.com/queenjuan/p/5578551.html

mailto 附帶附件_我和我的朋友如何將附帶項目發展為每月$ 17,000的業務

mailto 附帶附件In 2014, my friends and I set out to build the best possible web design tools. We built UI kits, Admin Dashboards, Templates, and Plugins. We’ve always tried to create products that are helpful in the development process, and that we oursel…

轉:PHP應用性能優化指南

程序員都喜歡最新的PHP 7,因為它使PHP成為執行最快的腳本語言之一(參考PHP 7 vs HHVM 比較)。但是保持最佳性能不僅需要快速執行代碼,更需要我們知道影響性能的問題點,以及這些問題的解決方案。本文涵蓋了保障PHP應用平…

java 運行異常處理_Java編程異常處理和I/O流

重點:  1.在編寫程序時,要正確地使用捕獲例外和聲明拋出異常的兩種例外處理的方法。2.遇到實際問題時,要根據需要正確使用各種輸入/輸出流,特別是對中文使用適當的字符輸入流。3.正…

反射練習

1.反射 一種計算機處理方式。是程序可以訪問、檢測和修改它本身狀態或行為的一種能力。 新建一個Person類: public class Person { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age age; } pu…

開源 物聯網接入_我們剛剛推出了開源產品。 那么接下來會發生什么呢?

開源 物聯網接入by Victor F. Santos由Victor F.Santos 我們剛剛推出了開源產品。 那么接下來會發生什么呢? (We just launched an open source product. So what happens next?) Last month me and the ninja god Pedro launched GitShowcase, a plug-and-play p…

Class? getClass()

getClass()方法屬于Object的一部分,它將產生對象的類,并且在打印該類時,可以看到該類類型的編碼字符串,前導"["表示這是一個后滿緊隨的類型的數組,而緊隨的"I"表示基本類型int, //: initialization/OptionalTrailingArgrments.java package object;import …