//Following function add new table entry to table
//and return interface which has function which uses closure to access and update the table
var _newRow = (function(){
var _interface = {
updateName: null,
updateProgress: null,
actionLinkButton : null,//<a> tag used for user aciton to perform on UI like delete, hide, show etc.
..
..
..
};
var tr = createTr();
var tdName = createTd();
_inteface.updateName = function(newName){
tdName.innerHTML = newName;
}
..
..
..
..
..
return _interface;
})(tblObject);
//maintaining the array of all the rows as per row number
rowArray[rowNo] = _newRow;
..
..
//using the row array to update the entries
rowArray[rowNo].updateProgress('read');
上面是我用来更新客户端动态添加的行的模式。我所做的就是在向表中添加行时创建 _interface,返回它并根据行号存储它。
然而,为此我使用了闭包意味着许多活着的对象。我想知道这是正确的方法吗?还有比这更好的方法吗?我可以使用哪些分析工具来了解此代码使用了多少内存?我怎样才能确保在不需要时正确清除关闭件?
JavaScript 有一个垃圾收集器,它会收集杂散对象并自动为您释放它们。没有办法控制何时或如何。
当您拥有全局可访问的对象引用具有词法作用域的函数时,可能会阻止对象/闭包被垃圾收集。确保分离所有不使用的值(例如,从 DOM 中删除它们)。如果您确定无法访问这些值,它们最终将被垃圾收集。
要识别内存泄漏,重要的是不要过早优化,您可以监视 Web 浏览器的内存使用情况。如果那里不明显,那么您可能不需要担心。你可能想在你的应用程序中模拟很多操作,看看应用程序运行很长时间后的状态会是什么样子。
但一般来说,不用担心内存使用情况。根据我的经验,这很少是一个问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)