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

 

你可能感兴趣的:(javascirpt)