【发布时间】:2015-12-17 11:56:17
【问题描述】:
我有以下代码:
var TagCloud = (function() {
var createTagsArray = function() {
j$("#" + this.cloudId + " a").each(function(index) {
//bla bla
});
}
function TagCloud(cloudId) {
this.cloudId = cloudId;
};
TagCloud.prototype.createTagCloud = function(){
createTagsArray(this.cloudId);
};
return TagCloud;
}());
new TagCloud("tagcloud");
我创建 TagCloud 对象。我用闭包创建它以拥有一个私有函数createTagArray。但是在这个函数中,我可以访问 TagCloud 构造函数中定义的变量 - cloudId。当然,在这个例子中,我无法使用 this 获得它。但是有没有办法得到它? (我不想将此值作为函数 createTagArray 参数传递)。
这也是我理解闭包的错误,因为我已经开始使用闭包了。
【问题讨论】:
-
您通常如何称呼
createTagsArray?这会被TagCloud构造函数调用吗? -
看看我的编辑。我想从公共函数 createTagCloud 中使用它
标签: javascript closures