当前位置: 代码迷 >> Web前端 >> 函数表达式的定义有关问题
  详细解决方案

函数表达式的定义有关问题

热度:87   发布时间:2012-10-24 14:15:58.0
函数表达式的定义问题
var f = function g(){ alert(22222)};

今天看到这段代码,虽然不这么写,但对这种形式产生了兴趣,它是怎么运行的?
测试发现,ie和火狐下还真不一样;
测试:
var f = function g(){ alert(22222)};
alert(f);
alert(f());
alert(g);
alert(g())

结果:ie:
function g(){ alert(22222)};
22222
undefined
function g(){ alert(22222)}
22222
undefined

firefox:
function g(){ alert(22222)};
22222
undefined
报错:g不存在


自己总结:ie下,相当于定义了两个变量f和g
就像:
var f = function (){ alert(22222)}
var g = function (){ alert(22222)}
firefox下:
g没有定义,什么也不是
  相关解决方案