【发布时间】:2011-09-25 20:57:19
【问题描述】:
考虑一下:
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)
为什么?
【问题讨论】:
标签: javascript oop function