简体   繁体   English

如何在 JavaScript 中创建数据结构?

[英]How to create data structure in JavaScript?

I am a C# developer that is trying to get his hands on JavaScript as well.我是一名 C# 开发人员,也正在尝试使用 JavaScript。

For a data handling program, I want to create a data structure like:对于数据处理程序,我想创建一个数据结构,如:

id,
begin,
end,
organiser,
    name,
    city,
    postalcode,
    street,
venue,
    name,
    city,
    postalcode,
    street

Coming from C#, I would create a "class" (gig.js) for the main structure:来自 C#,我将为主要结构创建一个“类”(gig.js):

function Gig(gig)
{
  this.id = gig.id;
  this.begin = gig.begin;
  this.end = gig.end;
  this.organiser = gig.organiser;
  this.venue = gig.venue;
}

and a "class" (address.js) for organiser and venue:以及组织者和场地的“类”(address.js):

function Address(a)
{
  this.name = a.name;
  this.city = a.city;
  this.postalcode = a.postalcode;
  this.street = a.street;
}

As I need to refer to the Address-class in the Gig-class, I wanted to reference the address-class in the main class, like因为我需要在 Gig-class 中引用 Address-class,所以我想在主类中引用 address-class,比如

include address.js包括地址.js

I have searched through the web and to my huge surprise, there seems to be no include mechanism at all.我在网上搜索过,令我惊讶的是,似乎根本没有包含机制。 HTML5 introduced a way to include JavaScript in another JavaScript, but is seems like no browser is supporting this so far I red some possible solutions here but they all look like hacks for a (from what I expected) simple one line code. HTML5 引入了一种将 JavaScript 包含在另一个 JavaScript 中的方法, 但到目前为止似乎还没有浏览器支持这一点,在这里提出了一些可能的解决方案,但它们看起来都像是(根据我的预期)简单一行代码的黑客攻击。

So, how do you do using another JavaScript-File nowadays?那么,您现在如何使用另一个 JavaScript 文件? Do I need to use an other architectural approach for this?我是否需要为此使用其他架构方法? Do I simply include them in the calling html and when I write the code for the main class, I simply hope, that the address class was loaded as well?我是否只是将它们包含在调用 html 中,当我为主类编写代码时,我只是希望地址类也被加载了?

Thanks in advance,提前致谢,
Frank坦率

Simple approach: embed the classes in HTML:简单的方法:在 HTML 中嵌入类:

<script src="path/to/address.js"></script>
<script src="path/to/gig.js"></script>
<script>
    // address is loaded before gig (sync loading), and now can use both
</script>

for more complex dependencies: A common way to approach it is bundling your modules using browserify对于更复杂的依赖项:一种常见的方法是使用browserify捆绑模块

Edit: Since latter seems to be what you're looking for, here's an example:编辑:由于后者似乎是您要查找的内容,因此这里有一个示例:

src/gig.js:源代码/演出.js:

// require dependencies
// one of your source files
var Address = require('./src/address');
// npm package dependency as installed by npm i someLib --save-dev
var someLib = require('someLib');

var Gig = function() {
    this.address = new Address();
}
// provide dependency
export.Gig = Gig;

main.js:主要.js:

var exampleStuff = require('src/Gig');
go();

You install browserify and additional modules via npm, then bundle:您通过 npm 安装 browserify 和其他模块,然后捆绑:

browserify main.js -o dist.js

then include dist.js in your html <script> tag然后在你的 html <script>标签中包含 dist.js

Use RequireJS .使用RequireJS Requirejs is a library that provides a nice interface for handling dependencies. Requirejs 是一个库,它提供了一个很好的接口来处理依赖项。 There is a nice tutorial for it here .有一个很好的教程它在这里 Other than that, you are right that there is currently no implemented way to enforce dependencies in JS, other than to hope that whoever is using your JS file has added the right source elements in the html.除此之外,您是对的,目前没有在 JS 中强制执行依赖关系的实现方法,除了希望使用您的 JS 文件的人在 html 中添加了正确的源元素。 This is why JavaScript libraries are commonly condensed to one file.这就是 JavaScript 库通常被压缩为一个文件的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM