zoukankan      html  css  js  c++  java
  • javascript易错点

    1.标题不知道叫什么好。

    2.一些比较绕的问题,可能出现在面试中。

      1 <!DOCTYPE html>
      2 <html>
      3     <head>
      4         <meta charset="UTF-8">
      5         <title></title>
      6     </head>
      7     <body>
      8        <script>
      9 //1题
     10        console.log(
     11            (function(){
     12                return typeof arguments;
     13            })());        //object
     14        //arguments本身就是一个对象,类似数组,实际上是当前函数的内置属性。arguments对象的长度是由实参个数而不是形参个数决定的。
     15            
     16 //2题           
     17        var f = function g(){
     18                console.log(typeof g);  //这里可以访问到g();
     19                return 23
     20            }
     21 //           console.log( g());     //error
     22            // 有名函数表达式创建了两个不同的函数,他们的作用域不同。
     23 
     24 
     25 //3题
     26            console.log(
     27                (function(x){
     28 //             console.log(delete x);    //false
     29                delete x;
     30               return x;
     31             })(1)
     32            );        //1
     33            //当一个属性不能被删除时,delete只返回false。
     34         //与函数参数相对应的创建的属性有DontDelete 特性,因此不能被删除。http://www.cnblogs.com/jfp888/archive/2011/06/09/2076127.html        
     35 //4题           
     36            
     37            var y = 1, x = y = typeof x;
     38            console.log(x);  //undefined
     39            // 链式赋值
     40 
     41 //5题
     42     var s= (function f(f){
     43                return typeof f();
     44            })(function(){return 1;})
     45        console.log(s);  //number
     46        
     47        //第二个括号定义了函数,并没有执行,参数就是整个函数,调用f()的时候,按照链式作用域的优先级,传过来的参数优先级高于函数本身。
     48 
     49 //6题
     50        var foo = {
     51               bar: function() {
     52                 return this.baz; 
     53               },
     54               baz: 1
     55             };
     56             
     57         s=(function(){        
     58               return typeof arguments[0]();
     59             })(foo.bar);
     60         console.log(s);   //undefined  
     61         //this关键字指向的是调用他的上下文环境的对象。foo.bar作为参数传给匿名函数时候,this指向了调用他的匿名函数,找不到this.baz;
     62 
     63 //7题
     64         var foo = {
     65           bar: function(){
     66             return this.baz; 
     67           },
     68           baz: 1
     69         }
     70         console.log(typeof (f=foo.bar)() );  //undefined        
     71         // 赋值语句作为函数体,不用想都不对。
     72 
     73 //8题
     74         var f = (
     75           function f(){ 
     76             console.log("111")
     77             return "1" 78           }, 
     79           function g(){ 
     80             console.log("2222")
     81             return 2 82           }
     83         )();
     84         console.log(f);
     85       console.log(typeof f);  //number      
     86       //匿名函数里有多个函数的时候,只执行了最后一个;  应该是括号找匿名函数体的时候,后一个覆盖前一个。具体执行过程待查询。
     87 
     88 //9题
     89       var x = 1;
     90         if (function k(){}) {        
     91           x += typeof k;
     92         }
     93         console.log(x); //1undefined        
     94         //条件是函数体 ,就是代码本身,所以if条件总是成立。但是条件中的函数,在内外部作用域中都找不到。        
     95 
     96 //10题
     97         var n = [typeof n, typeof m][1];
     98         console.log(typeof n);   //string
     99         console.log(typeof typeof n);  //string        
    100         //typeof 函数的返回值为string    
    101 
    102 //11题
    103     console.log(
    104         (function(foo){
    105             console.log("foo",foo.bar)
    106           return typeof foo.bar;
    107         })({ foo: { bar: 1 } })
    108     );   //undefined    
    109     //传过去的是 {foo:{bar:1}}这个对象 ,所以函数中的foo,就是它,对于函数中的foo,只有foo这一个属性,没有bar属性。  属性的层级问题。
    110 
    111 
    112 //12题
    113     console.log(
    114     (function f(){
    115       function f(){ return 1; }
    116       return f();
    117       function f(){ return 2; }
    118     })()
    119     ); //2 
    120     //相同作用域下函数声明提升,声明的时候后边的覆盖前边的,运行的时候,是执行不到return之后的语句的。
    121 
    122 
    123 //13题    
    124      function f(){return f; }
    125     //    console.log(new f() instanceof f);  //false
    126     
    127     
    128 //14题
    129      with (function(x, undefined){}) {var h=length};
    130     console.log(h); //2
    131     
    132     //与(function(x,y){}).length;相同,with指定了代码作用域,书写方便,效率降低。
    133       </script>
    134     </body>
    135 </html>
  • 相关阅读:
    uva 10369 Arctic Network
    uvalive 5834 Genghis Khan The Conqueror
    uvalive 4848 Tour Belt
    uvalive 4960 Sensor Network
    codeforces 798c Mike And Gcd Problem
    codeforces 796c Bank Hacking
    codeforces 768c Jon Snow And His Favourite Number
    hdu 1114 Piggy-Bank
    poj 1276 Cash Machine
    bzoj 2423 最长公共子序列
  • 原文地址:https://www.cnblogs.com/chengyunshen/p/7277339.html
Copyright © 2011-2022 走看看