JSLint 编写引用静态变量的构造函数

发布于 2024-11-03 21:47:49 字数 1011 浏览 4 评论 0原文

我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-11-10 21:47:49

JSLint 这里实际上是在强调代码中更广泛的问题,但没有明说。

您正在引用一个类(MemDisplay),但从未将其实例化为对象。即,您将类视为已经实例化的对象。

我创建了一个非常简单的相当于您想要实现的目标(也在this JSFiddle

function MyClass(p1, p2){
    this.param1 = p1;   //class member/property - use this to access internally.
    if (this.param1 === 1){ //you might want to consider doing this as part of some setter method
        alert("test");
    }
    this.MyMethod = function(){ //class method/function
        alert("MyMethod Called");
    };
}

var myObj = new MyClass(1,2); //instantiate
alert(myObj.param1); //get value of object member (you can set as well)
myObj.MyMethod(); //call a method

)需要进行一些重组,但是通过预先声明值,您可以让 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)

function MyClass(p1, p2){
    this.param1 = p1;   //class member/property - use this to access internally.
    if (this.param1 === 1){ //you might want to consider doing this as part of some setter method
        alert("test");
    }
    this.MyMethod = function(){ //class method/function
        alert("MyMethod Called");
    };
}

var myObj = new MyClass(1,2); //instantiate
alert(myObj.param1); //get value of object member (you can set as well)
myObj.MyMethod(); //call a method

It'll take a bit of reorgansiation, but by declaring the values up front, you can get make JSLint happy.

今天小雨转甜 2024-11-10 21:47:49

我的大脑一定是在我睡觉的时候弄清楚了这一点:诀窍是将字段附加到原型上,现在我已经想到了这一点,这似乎很明显,因为这就是定义类方法所必须做的。

以下在 JSLint 中进行检查,并演示了 MyClass 的所有实例之间共享字段(或参见 jsfiddle 上的此代码):

/*global alert */

function MyClass(name) {
    this.name = name;
    MyClass.prototype.field += 1;
}

MyClass.prototype.field = 0;

MyClass.prototype.myMethod = function () {
    alert(this.name + "'s class's field is " + MyClass.prototype.field);
};

var myObj = new MyClass("first");
myObj.myMethod();

var myOtherObj = new MyClass("second");
myObj.myMethod();
myOtherObj.myMethod();

我不确定是否有更好的方法来做到这一点,因为到处都有“原型”感觉有点过分,但另一方面它可能是一件好事,因为它使原型变得清晰。字段不属于该实例。

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):

/*global alert */

function MyClass(name) {
    this.name = name;
    MyClass.prototype.field += 1;
}

MyClass.prototype.field = 0;

MyClass.prototype.myMethod = function () {
    alert(this.name + "'s class's field is " + MyClass.prototype.field);
};

var myObj = new MyClass("first");
myObj.myMethod();

var myOtherObj = new MyClass("second");
myObj.myMethod();
myOtherObj.myMethod();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文