如何将可重复使用的JavaScript代码移动到单独的文件中,而不是主文件并使用几次?

发布于 2025-02-12 22:08:14 字数 1282 浏览 3 评论 0原文

我有一个具有许多属性的数据表。每次我想使用它时,我都必须复制它们。这使我的视图在巨大的混乱背后。因此,我决定创建一个单独的.js文件并在此处复制可重复使用的代码,但我无法在主文件中引用它。我的.js代码的某些部分就像Bellow:

      function loadDataTable() {

        $(".yadcf-filter-range-date").persianDatepicker();
        dataTable = $('#SP1').DataTable({
            serverSide: true,
            processing: true,
            pageLength: 10,
            infoFiltered: true,
            orderMulti: false,
            scrollX: true,
            scrollY: true,
            bStateSave: true,
            columnDefs: [{ orderable: false, targets: [15] }],
             .
             .
             .
       }

我在其他JS文件中创建一个函数:例如Bellow:

    function CommonPart(i) { 
            serverSide: true,
            processing: true,
            pageLength: 10,
            infoFiltered: true,
            orderMulti: false,
            scrollX: true,
            scrollY: true,
            bStateSave: true,
            columnDefs: [{ orderable: false, targets: [i] }]
           }

我不知道如何在主代码中使用Common Part,例如Bellow:

      function loadDataTable() {

        $(".yadcf-filter-range-date").persianDatepicker();
        dataTable = $('#SP1').DataTable({
        CommonPart(15);
        ...
         });

I have a DataTable with a lot of properties. Every time I want to use it, I have to copy all of them. It makes my view's code behind a huge mess. So I decide to create a separate .js file and copy the reusable code there but I can not reference it in my main file. Some part of my .js code is like bellow:

      function loadDataTable() {

        $(".yadcf-filter-range-date").persianDatepicker();
        dataTable = $('#SP1').DataTable({
            serverSide: true,
            processing: true,
            pageLength: 10,
            infoFiltered: true,
            orderMulti: false,
            scrollX: true,
            scrollY: true,
            bStateSave: true,
            columnDefs: [{ orderable: false, targets: [15] }],
             .
             .
             .
       }

I create a function in another js file for example like bellow:

    function CommonPart(i) { 
            serverSide: true,
            processing: true,
            pageLength: 10,
            infoFiltered: true,
            orderMulti: false,
            scrollX: true,
            scrollY: true,
            bStateSave: true,
            columnDefs: [{ orderable: false, targets: [i] }]
           }

I do not know how to use CommonPart in my main code for example like bellow:

      function loadDataTable() {

        $(".yadcf-filter-range-date").persianDatepicker();
        dataTable = $('#SP1').DataTable({
        CommonPart(15);
        ...
         });

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

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

发布评论

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

评论(2

空城缀染半城烟沙 2025-02-19 22:08:14

我首选的方法是用 ecmascript模块

您可以将每个功能都放入单独的文件中。您可以将文件命名为您喜欢的每个文件,但是我通常在包含的函数之后将它们命名。对于Web,我将给文件一个.js文件扩展名。在节点中,我会给它一个.mjs扩展名。

在每个文件中,prepend export export 这样的功能:

export function funcName()
{
}

然后从主代码文件中,您可以 import> import 您的函数您从这些独立文件中的函数

import { funcName } from "./fileName.mjs";

之后,您可以将函数像在主代码文件中声明一样(因为现在已导入)。

My preferred way to accomplish this is with ECMAscript modules.

You can place each of those functions into a separate file. You can name the files what every you like, but I usually name them after the function it contains. For web, I'd give the file a .js file extension. In node, I'd give it a .mjs extension.

In each file, prepend export on each function like this:

export function funcName()
{
}

Then from your main code file, you can import your functions from those separate files like this:

import { funcName } from "./fileName.mjs";

After this, you can use the functions as if they were declared in your main code file (because they are now imported).

随风而去 2025-02-19 22:08:14

最好,最受信任的资源ID MDN WebDocs
链接: https:// https://developer.mozilla.org/en-en--en--en-------- US/DOCS/WEB/JAVASCRIPT/GUIDE/模块

如果您仍然感到困惑随时提出问题。

The best and most trusted resource id MDN webdocs
link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

if you are still confused feel free to ask question..

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