當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
javascript 不让成为nan_这10个JavaScript面试题,看看你会几个?
生活随笔
收集整理的這篇文章主要介紹了
javascript 不让成为nan_这10个JavaScript面试题,看看你会几个?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
英文|?https://medium.com/javascript-in-plain-english/10-javascript-interview-questions-for-2020-697b40de9480翻譯|?web前端開發(ID:web_qdkf)的JavaScript正在成為世界上最受歡迎的編程語言。隨著對JS開發人員的需求增長,你必須為此做好準備。以下是你夢寐以求的十大JavaScript的基礎面試題。
1,var和let有什么區別?
盡管這似乎很容易,但您不會相信我因為沒有回答這個問題而不得不拒絕多少候選人。區別在于范圍等級。var是功能范圍的,但let(和const)是塊范圍的。要了解差異,請查看以下示例:function doStuff() { // both a and b will be available for this function, but not outside let a = 5; var b = 5; console.log(a + b); // 10}doStuff(); // 10;console.log(a); // ReferenceErrorconsole.log(b); // ReferenceErrorfunction doMoreStuff() { if (2 + 2 === 4) { // Here, a will be available for the whole function var a = 5; // But b will be available only inside this if block let b = 5; } console.log(a); // 5 console.log(b); // ReferenceError}doMoreStuff();// 5// ReferenceErrorfor (let i = 0; i < 5; i++) { // i is reccreated on every interation // and setTimeout gets a new i every time setTimeout(() => console.log(i), 100);}/*01234*/for (var j = 0; j < 5; j++) { // j is scoped to the outside function (which is the file itself) // thus, it is not recreated and setTimeout gets the same reference to j setTimeout(() => console.log(j), 100);}/*55555*/2,==和===有什么區別?
如果您的答案是“ ==按值比較,===也按類型比較”,那是不正確的。JS引擎的理解方式,==允許強制類型和===允許。類型強制是解釋器的自動類型轉換。這是JS中最令人驚奇的原因([] ==![]確實如此)。您可以在以下代碼段中觀察到差異:/* Here, '5' will be converted to 5 */5 == '5'; // true5 === '5'; // false/* Here, true will be converted to 1 */1 == true; // true1 > false; // true0 === false; // false// Here, JS will try to convert both of these to number// Number('true') = NaN (Not a Number), but Number(true) = 1'true' == true; // false'true' === true; // false3,“ this”關鍵字是什么意思?
您可能很想回答這個問題,它指向類體內的類實例,而不是對。首先,JS中的類是語法糖,不會釋放任何新功能。此關鍵字在任何函數中均可用,并指向包含此函數的對象。通過一個示例可能更容易理解:const myObject = { a: 'b', b: 'c', doStuff: function() { // Here, this refers to myObject console.log(this.a + this.b); }}myObject.doStuff(); // bc// BUT:const anotherObject = { a: 'abacaba', b: '!'};anotherObject.doStuff = myObject.doStuff;anotherObject.doStuff(); // abacaba!// Arrow functions do not have their own this and refer to the outer one:const arrowObject = { a: 'b', // here, this refers to the root function (file itself), which has no idea about a doMoreStuff: () => console.log(this.a)};arrowObject.doMoreStuff(); // undefined4,什么是構造函數?
JS中的構造函數也不是與類相關的函數,并且與此關鍵字緊密相關。使用新關鍵字調用構造函數,并返回任何此值。請注意,在構造函數this中,函數并不指向外部對象,,甚至使用占位符對象:function Car(name, make) { // Here, this is not a reference to outer object // But a placeheloder object you can use to construct the // desired value this.name = name; this.make = make; // you do not have to return anything, as this is automatically returned}const myCar = new Car('Outback', 'Subaru');console.log(myCar.name); // Outback5,如何將基于某些的函數轉換為基于Promise的函數?
解決問題更多的是練習,但至少是你必須知道。某些函數只是你打算在以后調用的簡單函數。當你必須等待某些事情(例如來自API的響應)時,通常會使用它們。但是,基于根本函數的代碼太復雜了,這就是為什么約會Promises函數的原因。
我不在這里討論,但是如果你現在知道承諾是什么,請查看本文(地址https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)。在下一個示例中,要求你將getData數據函數轉換為諾言:// the function itselffunction getData(callback, errorCallback) { try { // Do some network/api stuff... callback(result) } catch (e) { errorCallback(e); }}// Here is how you would use it:getData(result => console.log(result), error => console.error(error));// Here is how to create a Promise-based function from it:function getDataAsync() { return new Promise((resolve, reject) => { getData(resolve, reject); });}getDataAsync() .then(result => console.log(result)) .catch(error => console.error(error));// ORasync functoin main() { const result = await getDataAsync(); console.log(result);}無極構造函數接受一個回調,該回調接收兩個函數:決心和拒絕在回調內部,你可以執行耗時的任務,并根據結果調用。`決心`或`reject`。6,NaN === NaN?
簡而言之,NaN代表“不是數字”,僅因為一個值不是數字,而另一個值不是數字并不意味著這些變量。缺點是您無法真正檢查變量是否正在使用NaN使用myVariable === NaN。您可以使用該Number.isNaN功能或myVariable!== myVariable進行檢查。7、0.1 + 0.2 === 0.3?
假。這個技巧完全適用于JS:在任何語言的浮點運算中都很常見。它與CPU處理浮點數的方式有關。實際值0.1 + 0.2到0.300000001并檢查是否,你將編寫Math.abs (0.3-(0.2 + 0.1))<= EPS,其中EPS是一個任意小的值(例如0.00001)。8,JS中的原始數據類型是什么?
JS中的原始數據類型是不是對象且沒有方法的數據。這是JS中的原始數據類型的列表:布爾型
空值
未定義
數
大整數
串
符號
9,什么是“嚴格”模式?
在JS中,您可以通過“使用嚴格”;在文件的開頭放置嚴格的模式。嚴格模式可以在代碼中進行更嚴格的錯誤檢查,以便調試更加容易。例如,此代碼段可在常規JS中使用,但不嚴格:x = 'abacaba'; // using an undeclared variable is not allowed in strict modedelete x; // deleting variables is also not allowedfunction(x1, x1) {} // duplicating argument names is not allowed10,該代碼的輸出是什么?
function abacaba() { return { a: 'b' }}console.log(abacaba());undefined。發生這種情況是因為JS將在第2行的“返回”之后插入分號,變為第3-5行認為作用域而不是對象定義。總結在此之后,感謝你的閱讀,也希望今天的文章對你有所幫助,祝你在面試中一切順利!總結
以上是生活随笔為你收集整理的javascript 不让成为nan_这10个JavaScript面试题,看看你会几个?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php高并发网站指的是什么意思
- 下一篇: gradle idea java ssm