zoukankan      html  css  js  c++  java
  • Javascirpt 常见的误区

    var foo = new Object();
    var bar = new Object();
    var map = new Object();
    
    map[foo] = "foo";
    map[bar] = "bar";
    
    // Alerts "bar", not "foo".
    alert(map[foo]);

    当对象作为key的时候,会自动调用对象的 toString 方法,比如 

    map[foo]相当于 map[foo.toString()]即是 map["[object Object]"]  。
    可以重写toString方法
    // (1) Look up value by name:
    map.meaning_of_life;
    
    // (2) Look up value by passing the key as a string:
    map["meaning_of_life"];
    
    // (3) Look up value by passing an object whose toString() method returns a
    // string equivalent to the key:
    var machine = new Object();
    machine.toString = function() { return "meaning_of_life"; };
    map[machine];

    单引号和双引号的作用是相同的,在严格的JSON中,要使用双引号。同时最后一个key-value之后不能有逗号

    function hereOrThere(){
      return'here';
    }
    
    alert(hereOrThere());// alerts 'there'
    
    function hereOrThere(){
      return'there';
    }
    var hereOrThere = function() {
      return 'here';
    };
    
    alert(hereOrThere()); // alerts 'here'
    
    hereOrThere = function() {
      return 'there';
    };
    var a=1;
    function test(){
      console.log(a);
      var a=2;
      console.log(a);
    }
    

      输出结果 

    undefined 

    2

    参考http://bolinfest.com/javascript/misunderstood.html?utm_campaign=Manong_Weekly_Issue_9&utm_medium=EDM&utm_source=Manong_Weekly

     JS 将参数arguments 转为数组 例如 name=Array.prototype.splice.call(arguments,0)

    var set='ddd';
    function set(){
    console.log("hello");
    };

    set(); 

    提示错误 string  not  a  function

     
  • 相关阅读:
    SDOI2019游记
    noi.ac309 Mas的童年
    51nod1237 最大公约数之和
    loj6074 子序列
    noi.ac89A 电梯
    51nod97B 异或约束和
    bzoj4490 随机数生成器Ⅱ加强版
    CF55D Beautiful numbers
    CF24D Broken robot
    CF226D The table
  • 原文地址:https://www.cnblogs.com/dubaokun/p/3419218.html
Copyright © 2011-2022 走看看