如何记录 JavaScript/CoffeeScript 数据结构

发布于 2024-12-27 02:57:52 字数 449 浏览 4 评论 0原文

我正在寻找一种描述性方法来记录我的 JavaScript 应用程序中使用的数据结构。由于 JavaScript 的动态特性,我发现很难完成此任务。

例如,什么是一个好方法来判断所使用的变量 distance 是一个长度为 i 和 j 的二维数组,并存储 -1-1 之间的数字强>MAX_INT。我可以想到这样的事情:

distance[i][j] = -1 <= n <= MAX_INT

对于某些数据类型来说,用作 ma​​p/dictionary 的对象怎么样?二维数组怎么样,其中数组的第一个元素定义其他数据 。

当然,总是可以在文本中记录这些事情,我只是想,也许有一种众所周知且常用的方法可以以半正式的方式做到这一点

I am looking for a descriptive way to document the used data structures in my JavaScript application. I find it hard to get this done due to the dynamic character of JavaScript.

For instance, what could be a good way to tell, that a used variable distance is a two-dimensional array with length i and j and stores numbers between -1 and MAX_INT. I could think of something like this:

distance[i][j] = -1 <= n <= MAX_INT

What about an object which is used as a map/dictionary for certain data types, what about a two-dimensional array where the first element of an array defines other data then the rest, etc.

Of course, it is always possible to document these things in a text, I just thought, maybe there is a well known and used way to do this in a semiformal way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

天煞孤星 2025-01-03 02:57:52

尽管它还没有被广泛采用(还?),但有一个 JSON 架构标准草案。我只是自己学习,但您可以为二维数组(包装在对象内部)编写一个架构,如下所示:

{
    "description":"Two dimensional array of numbers",
    "type":"object",
    "properties":{
        "two-d-array":{
            "description":"columns",
            "type":"array",
            "items":{
                "description":"rows",
                "type":"array",
                "items": {
                   "description":"values",
                   "type":"number",
                   "minimum":-1,
                   "maximum":Number.MAX_VALUE
                }
            }
        }
    }
}

或者简单地说:

{
    "type":"array",
    "items":{
        "type":"array",
        "items": {
            "type":"number",
            "minimum":-1,
            "maximum":Number.MAX_VALUE
        }
    }
}

据我所知,没有 CoffeeScript 实现,但有几个 JavaScript 验证器的列表此处。我正在使用规范作者编写的名为(足够简单)的 json-schema 我很喜欢从 CoffeeScript 调用它。

Although it's not too widely adopted (yet?), there is a draft standard for JSON schema. I'm just learning it myself but you could write a schema for your two-dimensional array (wrapped inside of an object) as:

{
    "description":"Two dimensional array of numbers",
    "type":"object",
    "properties":{
        "two-d-array":{
            "description":"columns",
            "type":"array",
            "items":{
                "description":"rows",
                "type":"array",
                "items": {
                   "description":"values",
                   "type":"number",
                   "minimum":-1,
                   "maximum":Number.MAX_VALUE
                }
            }
        }
    }
}

or simply:

{
    "type":"array",
    "items":{
        "type":"array",
        "items": {
            "type":"number",
            "minimum":-1,
            "maximum":Number.MAX_VALUE
        }
    }
}

There is no CoffeeScript implementation that I know of, but there is a list of several JavaScript validators here. I'm playing with the one that's written by the spec authors called (simply enough) json-schema and I like it well enough calling it from CoffeeScript.

掐死时间 2025-01-03 02:57:52

当我复制大量数据模型时,我倾向于在 JavaScript 中做的就是在注释中写出它们的类定义。我不确定这是否是你的问题的意思。

// JavaScript Class jsHomeLocation
// jsHomeLocation{
// string name
// string address
// }

var jsHomeLocation = {};
jsHomeLocation.name = "Travis";
jsHomeLocation.address = "California";

您还可以使用javascript对象来跟踪示例的信息,二维数组

var distanceData = {};
distanceData.type = "two-dimensional array";
distanceData.length = i * j;
distanceData.min = -1;
distanceData.max = MAX_INT;

What I tend to do in my JavaScript when I am replicating a lot of data models is to write out what their class definition would be in comments. I am not sure if this is what you meant with your question.

// JavaScript Class jsHomeLocation
// jsHomeLocation{
// string name
// string address
// }

var jsHomeLocation = {};
jsHomeLocation.name = "Travis";
jsHomeLocation.address = "California";

You could also use javascript objects to track the information of the example, a two-dimensional array

var distanceData = {};
distanceData.type = "two-dimensional array";
distanceData.length = i * j;
distanceData.min = -1;
distanceData.max = MAX_INT;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文