Jquery 解决JSHint';s";Don';t使用';与'&引用;推荐
我正在使用一个2006年编写的脚本,并对其进行重写,以使其遵循最佳实践,并在将来包含到一个辅助项目中。我使用JSHint.com来解决问题,并搜索它发现的其他问题的解决方案。但是,我无法解决JSHint的“请勿使用with”错误。代码如下:Jquery 解决JSHint';s";Don';t使用';与'&引用;推荐,jquery,jslint,jshint,Jquery,Jslint,Jshint,我正在使用一个2006年编写的脚本,并对其进行重写,以使其遵循最佳实践,并在将来包含到一个辅助项目中。我使用JSHint.com来解决问题,并搜索它发现的其他问题的解决方案。但是,我无法解决JSHint的“请勿使用with”错误。代码如下: DragResize.prototype.select = function (newElement) { with(this) { // Selects an element for dragging. if (!
DragResize.prototype.select = function (newElement) {
with(this) {
// Selects an element for dragging.
if (!document.getElementById || !enabled) return;
// Activate and record our new dragging element.
if (newElement && (newElement != element) && enabled) {
element = newElement;
// Elevate it and give it resize handles.
element.style.zIndex = ++zIndex;
if (this.resizeHandleSet) this.resizeHandleSet(element, true);
// Record element attributes for mouseMove().
elmX = parseInt(element.style.left);
elmY = parseInt(element.style.top);
elmW = element.offsetWidth;
elmH = element.offsetHeight;
if (ondragfocus) this.ondragfocus();
}
}
};
我找到的唯一解释是这里的:,但我不知道如何将其应用于上述代码。有什么帮助吗?这段代码是一个很好的例子,说明了为什么使用
with
语句是如此糟糕!要解决JSHint警告,您需要知道with
语句主体中引用的哪些标识符实际上是DragResize
实例(this
)的属性,哪些标识符实际上是对外部范围中变量的引用
例如,如果元素
是实例的属性,则需要在这些引用前面加上this
:
DragResize.prototype.select = function (newElement) {
if (!document.getElementById || !enabled) return;
if (newElement && (newElement != element) && enabled) {
this.element = newElement;
// ^ prefix instance properties with reference to the instance
this.element.style.zIndex = ++zIndex;
if (this.resizeHandleSet) this.resizeHandleSet(element, true);
// ...
}
};
我已经在许多其他配置上尝试过,但都不起作用。(this)语句大约还有7个
。我应该发布整个脚本的链接吗?