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

 

转载于:https://www.cnblogs.com/dubaokun/p/3419218.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值