如何记录 JavaScript/CoffeeScript 数据结构
我正在寻找一种描述性方法来记录我的 JavaScript 应用程序中使用的数据结构。由于 JavaScript 的动态特性,我发现很难完成此任务。
例如,什么是一个好方法来判断所使用的变量 distance 是一个长度为 i 和 j 的二维数组,并存储 -1 和 -1 之间的数字强>MAX_INT。我可以想到这样的事情:
distance[i][j] = -1 <= n <= MAX_INT
对于某些数据类型来说,用作 map/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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管它还没有被广泛采用(还?),但有一个 JSON 架构标准草案。我只是自己学习,但您可以为二维数组(包装在对象内部)编写一个架构,如下所示:
或者简单地说:
据我所知,没有 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:
or simply:
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.
当我复制大量数据模型时,我倾向于在 JavaScript 中做的就是在注释中写出它们的类定义。我不确定这是否是你的问题的意思。
您还可以使用javascript对象来跟踪示例的信息,二维数组
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.
You could also use javascript objects to track the information of the example, a two-dimensional array