JSLint 编写引用静态变量的构造函数
我正在用 Javascript(使用 jQuery)编写一个显示类,它可以在加载网页之前实例化。如果调用构造函数时页面尚未准备好,则实例将添加到该类的静态 instances
字段中,该字段会在页面加载时进行迭代:
function MemDisplay(ready_callback) {
this.readyCallback = ready_callback;
if (MemDisplay.ready) {
this.linkToPage();
} else {
MemDislay.instances.push(this);
}
}
//this makes sure that the ready callback can be sent when the page has loaded
MemDisplay.ready = false;
MemDisplay.instances = [];
$(document).ready(function () {
var i;
MemDisplay.ready = true;
for (i = 0; i < MemDisplay.instances.length; i += 1) {
MemDisplay.instances[i].linkToPage();
} });
//example truncated for brevity
当我通过 JSLint 运行此操作时,我收到此错误:
第 25 行第 9 字符出现问题: “MemDislay”未定义。
MemDislay.instances.push(this);
我需要在构造函数中引用 MemDisplay.instances
,但构造函数是定义 MemDisplay
的地方,所以我很困惑如何在符合 JSLint 指南的情况下完成这项工作。有更好的方法吗?在这种情况下我应该忽略 JSLint 吗?
I'm writing a display class in Javascript (using jQuery) which may be instantiated before a web page has loaded. If the page isn't ready when the constructor is called, the instance is added to a static instances
field for the class, which is iterated over when the page has loaded:
function MemDisplay(ready_callback) {
this.readyCallback = ready_callback;
if (MemDisplay.ready) {
this.linkToPage();
} else {
MemDislay.instances.push(this);
}
}
//this makes sure that the ready callback can be sent when the page has loaded
MemDisplay.ready = false;
MemDisplay.instances = [];
$(document).ready(function () {
var i;
MemDisplay.ready = true;
for (i = 0; i < MemDisplay.instances.length; i += 1) {
MemDisplay.instances[i].linkToPage();
} });
//example truncated for brevity
When I run this through JSLint, I get this error:
Problem at line 25 character 9:
'MemDislay' is not defined.MemDislay.instances.push(this);
I need to reference MemDisplay.instances
in the constructor, but the constructor is where MemDisplay
is defined, so I'm puzzled about how to make this work while fitting within JSLint's guidelines. Is there a better way to do this? Should I just ignore JSLint in this instance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JSLint 这里实际上是在强调代码中更广泛的问题,但没有明说。
您正在引用一个类(MemDisplay),但从未将其实例化为对象。即,您将类视为已经实例化的对象。
我创建了一个非常简单的相当于您想要实现的目标(也在this JSFiddle
)需要进行一些重组,但是通过预先声明值,您可以让 JSLint 满意。
JSLint here is actually highlighting a broader issue with the code without saying so.
You are referencing a class (MemDisplay) but never instantiating it as an object. I.e. you are treating the class like an already-instantiated object.
I've created a very simple equivalent to what you are trying to achieve (also at this JSFiddle)
It'll take a bit of reorgansiation, but by declaring the values up front, you can get make JSLint happy.
我的大脑一定是在我睡觉的时候弄清楚了这一点:诀窍是将字段附加到原型上,现在我已经想到了这一点,这似乎很明显,因为这就是定义类方法所必须做的。
以下在 JSLint 中进行检查,并演示了 MyClass 的所有实例之间共享字段(或参见 jsfiddle 上的此代码):
我不确定是否有更好的方法来做到这一点,因为到处都有“原型”感觉有点过分,但另一方面它可能是一件好事,因为它使原型变得清晰。字段不属于该实例。
My brain must have figured this out while I slept: the trick is to attach the field to the prototype, which seems pretty obvious now that I've thought of it, since that's what you have to do to define class methods.
The following checks out in JSLint, and demonstrates the sharing of a field between all instances of MyClass (or see this code on jsfiddle):
I'm not sure if there's a prettier way to do it, as having 'prototype' all over the place feels a bit excessive, on the other hand it could be a good thing because it makes it clear that prototype.field does not belong to the instance.