var b = function a(n){
if(n>1)
return a(n-1)+1;
else
return 1;
};
var bb = b;
a = 3;
bb(3);
var b = function a(n){
if(n>1)
return a(n-1)+1;
else
return 1;
};
var bb = b;
a = 3;
bb(3);
此时,bb函数能正确返回我们想要的结果。
为了提高JavaScript函数的封装性,我们可以定义函数化的构造器,下面是一个例子:
复制代码 代码如下:
[javascript]
var funcCons = function(spec){
var that = {};
that.getName = function(){
return spec.name;
};
that.says = function(){
return spec.saying || '';
};
return that;
};
var myFunc = funcCons({name:'NearEast'});
var funcCons = function(spec){
var that = {};
that.getName = function(){
return spec.name;
};
that.says = function(){
return spec.saying || '';
};
return that;
};
var myFunc = funcCons({name:'NearEast'});
这样,我们可以在构造器中定义一些私有变量(如字典表)和函数,而不必把它们全部暴露在外面。
您可能感兴趣的文章: