zoukankan      html  css  js  c++  java
  • 【面试必读】一不注意就做错的五道JavaScript题目

    如果不会,可以存在DW中运行试一下哦~

    1、这段代码会输出什么?

    1. function Container( properties ) {  
    2.     var objthis = this;  
    3.     for ( var i in properties ) {  
    4.         (function(){  
    5.                 var t = properties[i];  
    6.                 objthis[ "get" + i ] = function() {return t;};  
    7.                 objthis[ "set" + i ] = function(val) {t = val;};  
    8.         })();  
    9.     }  
    10. }  
    11.    
    12. var prop = {Name : "Jim", Age : 13};  
    13. var con = new Container(prop);  
    14. console.log(con.getName());  
    15.    
    16. con.setName("Lucy");  
    17. console.log(con.getName());  
    18. console.log(prop.Name);  

    2、输出什么 ?

    1. function a (x) {  
    2.     return x * 2;  
    3. }  
    4. var a;  
    5. console.log(a);  

    这段代码中,其实var a并没有任何影响,输出的是a(x)这样的方法签名。

    3、

    1. c = 999;  
    2. var c = 888;  
    3. console.log(this.c); //①  
    4. function b (x, y, c) {  
    5.     c = 6;  
    6.     arguments[2] = 10;  
    7.     console.log(c); //②  
    8.     console.log(this.c); //③  
    9.        
    10.     var c = 6;  
    11.     console.log(c); //④  
    12.     console.log(this.c); //⑤  
    13. }  
    14. b(1234);  

    这道题是比较变态的。

    • 第①处,this指的是window,在window下,c、this.c、var c在这里指的是同一个东西,看透这一点就好办了。打印的是888。
    • 第②处,方法体中,参数c和arguments[2]指的是同一个地址,所以把后者赋为10的时候,打印的是10,不是6。
    • 第③处,this指的是window,打印的是888。
    • 第④处,打印的是6。
    • 第⑤处,打印的是888。

    4、

    1. var start = new Date();  
    2. setTimeout(  
    3.     function(){  
    4.         var end = new Date();  
    5.         console.log(end - start);  
    6.     },  
    7.     1000  
    8. );  
    9. while(new Date() - start < 2000); 

    JavaScript因为是单线程工作的,因此虽然回调函数设置了1000毫秒后执行,事实上在1000毫秒的时候根本得不到执行,等待到while循环执行完毕后(因此已经是2000毫秒以后了),才去执行,因此输出应该是一个大于2000的数字。

     

    5、 

    1. (function(){  
    2.     console.log(typeof arguments);  
    3. })(); 

    很多人会说打印的是array,其实,arguments不是数组,打印的是object。

  • 相关阅读:
    acceptorThreadCount
    spring boot tomcat 线程数 修改初始线程数 统计性能 每百次请求耗时
    java 获取当前进程id 线程id
    Linux操作系统中打开文件数量的查看方法
    java.io.IOException: Too many open files
    随机采样 算法
    Spring Boot
    您好,python的请求es的http库是urllib3, 一个请求到贵司的es节点,想了解下,中间有哪些网关啊?冒昧推测,贵司的部分公共网关与python-urllib3的对接存在异常?
    运行状态:锁定中(实例空间满自动锁)
    。。。。。。不带http https : 不报错 spring boot elasticsearch rest
  • 原文地址:https://www.cnblogs.com/ranzige/p/five_javascript_questions.html
Copyright © 2011-2022 走看看