Javascript 模块化大对象

发布于 2024-10-04 01:08:19 字数 494 浏览 5 评论 0原文

我有一个定义为函数的对象,其中有很多方法和属性,有哪些方法可以对其进行模块化以使其更易于管理?是否可以将对象内部的函数放入外部文件中?

编辑:

有人在一个答案中提到包含其他文件,但这会产生一个笨拙的 HTML 页面,IE:

<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
....
<script type="text/javascript" src="script76.js"></script>

是否还有任何方法来构造代码,以便只需要一个 javascript 文件的引用?

I've got an object defined as a function, and it's got a lot of methods and properties in it, what are some ways to modularise this to make it more manageable? It is possible to put functions inside the object into external files?

Edit:

Someone mentioned in one answer to include other files, but this would have an unweildy HTML page, IE:

<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
....
<script type="text/javascript" src="script76.js"></script>

Is there also any way to structure the code so only one reference is required the the javascript file?

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

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

发布评论

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

评论(4

魂归处 2024-10-11 01:08:19
var myobj = {
  somefunc: function() { /* function code */ },
  prop: "some string",
  // etc.
};

并在上述代码之后包含的另一个js文件中

myobj.anotherfunc = function () { /* function code */ };
var myobj = {
  somefunc: function() { /* function code */ },
  prop: "some string",
  // etc.
};

and in another js file included after the above code

myobj.anotherfunc = function () { /* function code */ };
橘和柠 2024-10-11 01:08:19

您可能需要查看 RequireJS。它允许您定义函数、模块、依赖项并从 Javascript 加载外部 Javascript 文件(可能是异步的)。

You may want to check out RequireJS. It lets you define functions, modules, dependencies and load external Javascript files from Javascript (possibly async).

难以启齿的温柔 2024-10-11 01:08:19

您可以使用服务器端脚本来处理您的 JavaScript,使用 jsmin 您甚至可以加快该过程(如果缓存)。

我正在考虑这样的脚本:
script.php

<?php 

$files = array('script1.js', 'script2.js');
$output = '';

foreach($files as $file){
    $output .= file_get_contents($file);
}

header("Content-type: application/x-javascript");
echo $output;

?>

your html
<script type="text/javascript" src="/script.php"></script>

您也可以将所有 js 文件包含在一个文件夹中,只需通过文件夹扫描或其他方式生成数组,我想您明白了!

问候内克苏姆

you could use a server side script to handle your javascripts, with jsmin you could even speed up the process (if cached).

i'm thinking of a script somthing like this:
scripts.php

<?php 

$files = array('script1.js', 'script2.js');
$output = '';

foreach($files as $file){
    $output .= file_get_contents($file);
}

header("Content-type: application/x-javascript");
echo $output;

?>

your html
<script type="text/javascript" src="/script.php"></script>

you could also just include all js files inside a folder, just generate the array via folder scanning or something, i think you get the idea!

greetings Nexum

迷迭香的记忆 2024-10-11 01:08:19

你可以试试这个:
http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm

...但最佳实践是仅在 html 主页面中包含您想要的文件

You could try this:
http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm

...but best practices are to just include the files you want in the main html page

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