<pre style="background-color:#2b2b2b; color:#a9b7c6; font-family:'Menlo'; font-size:10.5pt"><span style="color:#629755"><em>/**转:你有必要知道的25个JavaScript面试题
</em></span><span style="color:#629755"><em> *原文:http://www.jb51.net/article/77140.htm
</em></span><span style="color:#629755"><em> *看到这个文章,觉得js还有很多东西要弄明白。我把目前能接受的运行一下,这里做个记录吧。
</em></span><span style="color:#629755"><em> **/
</em></span><span style="color:#629755"><em>/**类型判断
</em></span><span style="color:#629755"><em> * 看来Object.prototype.toString.call这个比typeof更精准了,得记住。
</em></span><span style="color:#629755"><em> * */
</em></span><span style="color:#cc7832"><strong>function </strong></span><span style="color:#ffc66d">fun</span>() {
}
<span style="color:#cc7832"><strong>var </strong></span>obj = {}<span style="color:#cc7832">;
</span><span style="color:#cc7832"><strong>var </strong></span>arr = []<span style="color:#cc7832">;
</span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(<span style="color:#cc7832"><strong>typeof</strong></span>(obj))<span style="color:#cc7832">;</span><span style="color:#808080">//object
</span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(<span style="color:#cc7832"><strong>typeof</strong></span>(arr))<span style="color:#cc7832">;</span><span style="color:#808080">//object
</span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(<span style="color:#cc7832"><strong>typeof</strong></span>(<span style="color:#ffc66d">fun</span>))<span style="color:#cc7832">;</span><span style="color:#808080">//function
</span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(<span style="color:#cc7832"><strong>typeof</strong></span>(obj) === <span style="color:#cc7832"><strong>typeof</strong></span>(arr))<span style="color:#cc7832">;</span><span style="color:#808080">//true
</span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(Object.<span style="color:#9876aa">prototype</span>.<span style="color:#ffc66d">toString</span>.<span style="color:#ffc66d">call</span>(obj))<span style="color:#cc7832">;</span><span style="color:#808080">//[object Object]
</span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(Object.<span style="color:#9876aa">prototype</span>.<span style="color:#ffc66d">toString</span>.<span style="color:#ffc66d">call</span>(arr))<span style="color:#cc7832">;</span><span style="color:#808080">//[object Array]
</span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(Object.<span style="color:#9876aa">prototype</span>.<span style="color:#ffc66d">toString</span>.<span style="color:#ffc66d">call</span>(<span style="color:#ffc66d">fun</span>))<span style="color:#cc7832">;</span><span style="color:#808080">//[object Function]
</span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(Object.<span style="color:#9876aa">prototype</span>.<span style="color:#ffc66d">toString</span>.<span style="color:#ffc66d">call</span>(obj) === Object.<span style="color:#9876aa">prototype</span>.<span style="color:#ffc66d">toString</span>.<span style="color:#ffc66d">call</span>(arr))<span style="color:#cc7832">;</span><span style="color:#808080">//false
</span><span style="color:#808080">
</span><span style="color:#629755"><em>/**this作用域
</em></span><span style="color:#629755"><em> * 自动运行的函数它的调用这居然是window,这也就不奇怪了自运行函数的this.foo是为定义的。
</em></span><span style="color:#629755"><em> * */
</em></span><span style="color:#cc7832"><strong>var </strong></span>myObject = {
<span style="color:#9876aa">foo</span>: <span style="color:#6a8759">"bar"</span><span style="color:#cc7832">,
</span><span style="color:#cc7832"> </span><span style="color:#ffc66d">func</span>: <span style="color:#cc7832"><strong>function </strong></span>() {
<span style="color:#cc7832"><strong>var </strong></span>self = <span style="color:#cc7832"><strong>this</strong></span><span style="color:#cc7832">;
</span><span style="color:#cc7832"> </span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(<span style="color:#cc7832"><strong>this</strong></span>.<span style="color:#9876aa">foo</span>)<span style="color:#cc7832">;</span><span style="color:#808080">//bar
</span><span style="color:#808080"> </span><span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(self.<span style="color:#9876aa">foo</span>)<span style="color:#cc7832">;</span><span style="color:#808080">//bar
</span><span style="color:#808080"> </span>(<span style="color:#cc7832"><strong>function </strong></span>() {
<span style="color:#9876aa">console</span>.<span style="color:#ffc66d">log</span>(Object.<span style="color:#9876aa">prototype</span>.<span style="color:#ffc66d">toString</span>.<span style="color:#ffc66d">call</span>(<span style="color:#cc7832"><strong>this</strong></span>))<span style="color:#cc7832">;</span><span style="color:#808080">//[object Window]
</span><span style="color:#808080"> |
|