[java学习6]你有必要知道的25个JavaScript面试题

论坛 期权论坛     
选择匿名的用户   2021-5-23 02:02   0   0
<pre style="background-color:#2b2b2b; color:#a9b7c6; font-family:&#39;Menlo&#39;; 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 &#61; {}<span style="color:#cc7832">;
</span><span style="color:#cc7832"><strong>var </strong></span>arr &#61; []<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) &#61;&#61;&#61; <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) &#61;&#61;&#61; 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 &#61; {
    <span style="color:#9876aa">foo</span>: <span style="color:#6a8759">&#34;bar&#34;</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 &#61; <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">   
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP