javascript
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 falseTo 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中的輸出結果是?為什么?
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; 錯誤地理解為是以下聲明的簡寫:
But in fact, var a = b = 3; is actually shorthand for:
[譯]:但事實上,var a = b = 3是以下寫法的簡寫:
As a result (if you are not using strict mode), the output of the code snippet would be:
[譯]:因此(如果你沒有使用嚴格模式的話),該代碼段的輸出是:
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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 愿自己越来越好的说说238个
- 下一篇: 淘宝uv价值是什么意思