一段时间以来,我一直在使用两个版本的 JavaScript 模式,我从 Addy Osmani 那里学到了称为模块模式的模式。在这里查看
该模式的第一个版本使用对象字面量:
var x = {
b: function() {
return 'text';
},
c: function() {
var h = this.b();
h += ' for reading';
}
}
alert(x.b()) // alerts text.
而另一个版本使用自执行功能:
var y = (function() {
var first = 'some value';
var second = 'some other value';
function concat() {
return first += ' '+second;
}
return {
setNewValue: function(userValue) {
first = userValue;
},
showNewVal: function() {
alert(concat());
}
}
})();
y.setNewValue('something else');
y.showNewVal();
鉴于上面的示例,这两种模式中的任何一种(不考虑任何事件侦听器)是否对垃圾收集友好(考虑到它们引用自己的方式)?