【发布时间】:2011-01-16 14:43:05
【问题描述】:
使用以下代码:
function Node(){
....
function foo(request){
for (var name in this ) {
log(name +" is "+this[name]);
if(!(name == 'all' || typeof this[name] == 'function')){
request[name] = this[name];
}
}
return ...
};
}
我很惊讶当私有函数 foo 被调用时 this 似乎并没有引用包含对象(Node 的一个实例)。这是为什么呢?
当然我可以有类似的东西:
function Node(){
var ref = this;
....
}
并在foo 中将ref 用作this,但是有没有办法声明this 是对包含对象的引用的私有方法?
【问题讨论】:
标签: javascript methods private