【发布时间】:2013-06-20 03:21:12
【问题描述】:
这是我的部分代码,但是朋友说变量(如getStyle,getOffsetWidth,getOffsetHeight,log)不会释放,所以我想知道为什么变量不会释放,以及如何优化它,谢谢!
var Util = (function() {
"use strict";
var getStyle = function(node) {
var style = null;
if (window.getComputedStyle) {
style = window.getComputedStyle(node, null);
} else {
style = node.currentStyle;
}
return style;
};
var getOffsetWidth = function(style) {
return parseInt(style.width, 10) +
parseInt(style.paddingLeft, 10) +
parseInt(style.paddingRight, 10) +
parseInt(style.marginLeft, 10) +
parseInt(style.marginRight, 10);
};
var getOffsetHeight = function(style) {
return parseInt(style.height, 10) +
parseInt(style.paddingTop, 10) +
parseInt(style.paddingBottom, 10) +
parseInt(style.marginTop, 10) +
parseInt(style.marginBottom, 10);
};
var log = function() {
if (window.console && window.console.log) {
window.console.log(arguments);
}
};
return {
getStyle: getStyle,
getOffsetWidth: getOffsetWidth,
getOffsetHeight: getOffsetHeight,
log: log
};
}());
【问题讨论】:
-
"variable doesn't release"是什么意思,我不明白...
-
您可能希望将
}());更改为})();。除此之外,“不释放”是什么意思?你的朋友指的是什么变量? -
看看下面的页面,它解释了 javascript 内存管理并讨论了垃圾收集:developer.mozilla.org/en-US/docs/Web/JavaScript/…
-
@Blender
}());也是 valid syntax。 -
@canon:是的,这就是我说“可能”的原因。这是更常见的语法,至少在我看过的项目中是这样。
标签: javascript memory-leaks closures