考慮代碼:爲什麼Javascript不會讓函數本身重新定義自己?
window.a = function(x){
var r = x*2;
window.a =alert; // redefines itself after first call
return r;
};
a('2 * 2 = '+a(2)); // doesn't work. it should've alerted "2 * 2 = 4"
這不起作用或者:
window.a = function(x){
alert(x);
window.a = function(x){ // redefines itself after first call
var r = x*2;
return r;
}
};
a('2 * 2 = '+a(2)); // doesn't work. it should've alerted "2 * 2 = 4"
既不做到這一點:
window.a = function(x){ alert(x); window.c = window.a; window.a = window.b; window.b = window.c; };
window.b = function(x){ var r = x*2; window.c = window.b; window.b = window.a; window.a = window.c; return r; };
a('2 * 2 = '+a(2)); // doesn't work.
,基本上我已經嘗試了所有可能的方式也不似乎做的工作。有人可以解釋爲什麼嗎?
出於好奇,你爲什麼要這麼做? – maialithar 2012-03-30 11:07:35
嗯,第一個實際上適用於我(Chrome 18) – Yoshi 2012-03-30 11:10:28
在Chrome和IE中工作得很好... – 2012-03-30 11:11:21