考慮這個(?!?):的Javascript接力般的失敗
function f2(x) {
return x+1;
};
X = function(){
this.f1=function (x) {
return 2*f2(x);
}
return this;
};
然後x = new X(); x.f1(1)
工作正常。
但是,當我想這樣做:
X = function(){
this.f2 = function(x) {
return x+1;
};
this.f1=function (x) {
return 2*f2(x);
}
return this;
};
相同的語句會抱怨它不能找到F2。 在,例如C#,你可以說
class X {
int f2(int x){return x+1;}
int f1(int x){return 2*f2(x);}
}
,這將工作
X x=new X();
x.f1(1)
爲什麼?