日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Some Essential JavaScript Questions And Answers(1)

發布時間:2023/12/31 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Some Essential JavaScript Questions And Answers(1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一些很經典的JavaScript探討題,分享分享,英語好的可以忽略我的翻譯。

Some Essential JavaScript? Questions

[譯]:一些必要的(基本的)JS面試題及答案

Question 1:

What is a potential pitfall with using typeof bar === "object" to determine if bar is an object? How can this pitfall be avoided ?

[譯]:使用typeof bar === "object"判斷bar是不是一個對象存在什么潛在的缺陷?如何避免這個缺陷?

Answer:

Although?typeof bar === "object"?is?a reliable way of checking if?bar?is an object, the surprising gotcha in JavaScript is that?null?is?also?considered an object!

[譯]:雖然 typeof bar === "object" 是檢查bar是否是對象的可靠方法,但令人驚訝的是在JavaScript中 null 也是被視為對象的!

Therefore, the following code will, to the surprise of most developers, log?true?(not?false) to the console:

[譯]:因此,以下代碼將會使很多開發人員感到驚訝,因為在控制臺中輸出的是true而不是false。

var bar = null; console.log(typeof bar === "object"); // logs true!

As long as one is aware of this, the problem can easily be avoided by also checking if?bar?is?null:

[譯]:只要意識到這點,同時檢查bar是否為null的話,就能很容易地避免這個問題:

console.log((bar !== null) && (typeof bar === "object")); // logs false

To be entirely thorough in our answer, there are two other things worth noting:

First, the above solution will return?false?if?bar?is a function. In most cases, this is the desired behavior, but in situations where you want to also return?true?for functions, you could amend the above solution to be:

[譯]:要回答得全面的話,還有其他兩件事情值得注意:

第一,假如bar是一個函數的時候,上述解決方案將返回false。多數情況下,這是期望的行為,當你也想對函數返回true的話,可以將上述方案修改為:

console.log((bar !== null) && ((typeof bar === "object") || (typeof bar === "function")));

Second, the above solution will return?true?if?bar?is an array (e.g., if?var bar = [];). In most cases, this is the desired behavior, since arrays are indeed objects, but in situations where you want to also?false?for arrays, you could amend the above solution to be:

[譯]:第二,當bar是一個數組的時候,上述方案將返回true。多數情況下,這個預期的行為,因為數組實際上也是對象,當你想對數組返回false時,你可以將上述方案修改為:

console.log((bar !== null) && (typeof bar === "object") && (toString.call(bar) !== "[object Array]"));

However, there’s one other alternative that returns?false?for nulls, arrays, and functions, but?true?for objects:

[譯]:然而,這里有一種可替代的方案,當bar是null、數組、函數時會返回false,當bar時object時返回true。

console.log((bar !== null) && (bar.constructor === Object));

Or, if you’re using jQuery:

[譯]:如果你使用JQuery的話,還可以這樣:

console.log((bar !== null) && (typeof bar === "object") && (! $.isArray(bar)));

ES5 makes the array case quite simple, including its own null check:

[譯]:ES5使得數組的情況很簡單,包括它自身的null校驗:

console.log(Array.isArray(bar));


Question 2:

What will the code below output to the console and why?

[譯]:以下代碼在console中的輸出結果是?為什么?

(function(){var a = b = 3; })();console.log("a defined? " + (typeof a !== 'undefined')); console.log("b defined? " + (typeof b !== 'undefined'));
Answer:

Since both a and b are defined within the enclosing scope of the function, and since the line they are on begins with the var keyword, most JavaScript developers would expect typeof a and typeof b to both be undefined in the above example.? However, that is not the case. The issue here is that most developers incorrectly understand the statement var a = b = 3; to be shorthand for:

[譯]:由于 a 和 b 都定義在函數的封閉范圍內,并且都始于 var關鍵字,大多數JavaScript開發人員期望 typeof a 和 typeof b 在上面的例子中都是undefined。然而,事實并非如此。這里的問題是,大多數開發人員將語句 var a = b = 3; 錯誤地理解為是以下聲明的簡寫:

var b = 3; var a = b;

But in fact, var a = b = 3; is actually shorthand for:

[譯]:但事實上,var a = b = 3是以下寫法的簡寫:

b = 3; var a = b;

As a result (if you are not using strict mode), the output of the code snippet would be:

[譯]:因此(如果你沒有使用嚴格模式的話),該代碼段的輸出是:

a defined? false b defined? true

But how can b be defined outside of the scope of the enclosing function? Well, since the statement var a = b = 3; is shorthand for the statements b = 3; and var a = b;, b ends up being a global variable (since it is not preceded by the var keyword) and is therefore still in scope even outside of the enclosing function.

[譯]:但是,?b 是怎樣被定義在封閉函數的范圍之外的呢?是的,既然語句?var a = b = 3;?是語句?b = 3;?和?var a = b;的簡寫,?b?最終成為了一個全局變量(因為它沒有前綴?var?關鍵字),因此仍然在范圍內甚至封閉函數之外。(本人提醒:即例子中的b可以通過windows.b訪問到)

Note that, in strict mode (i.e., with use strict), the statement var a = b = 3; will generate a runtime error of ReferenceError: b is not defined, thereby avoiding any headfakes/bugs that might othewise result. (Yet another prime example of why you should use use strict as a matter of course in your code!)

[譯]:需要注意的是,在嚴格模式下(即使用了 use strict),語句var a = b = 3;?將生成運行時錯誤:ReferenceError: b is not defined,從而避免可能會導致的麻煩或者八阿哥。 (還是你為什么應該理所當然地在代碼中使用?use strict?的最好例子!)

總結

以上是生活随笔為你收集整理的Some Essential JavaScript Questions And Answers(1)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。