如何在另一个文件上调用具有相同名称的函数

问题描述 投票:0回答:1

我正在用html编写一个O.S,我的操作系统可以通过指定主脚本来支持应用程序,现在我有两个应用程序,一个执行file1,另一个执行file2。我有三个文件:

file1.js

function main() {
    //this is a code
    . . .
}

file2.js

function main() {
    //this is another code
    . . .
}

我想只调用他们的一个函数,即file1的函数:

System.js

var System = {
/*
 *Used Mode:
 *
 *  var app = System.openApp(package);
 */
openApp:function(package) {

    //starts the app:
    var path = "/@APP:/" + package + "/app.xml";

    console.log("Opening " + package + "...");
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", path, true);
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            console.log("The app \"" + xhttp.responseXML.getElementsByTagName("Manifest")[0].getElementsByTagName("AppName")[0].childNodes[0].nodeValue + "\" was successfully loaded!");
            if (xhttp.responseXML.getElementsByTagName("Manifest")[0].getElementsByTagName("AppMode")[0].childNodes[0].nodeValue = "prompted") {
                this.openMainAct();
            } 
        }
    };
    xhttp.onerror = function() {
        console.error("The app \"" + package + "\" was not found\n  ERROR_CODE:" + xhttp.readyState);
    };
    xhttp.send();

    //subfunctions:
    /*
     *Used Mode:
     *
     *  app.setIcon("path/to/file.png");
     */
    this.setIcon = function(icon) {
        // @TODO: Making Icons, just ignore;
    };
    /*
     *@DEPRECATED: Use the method "openAct"
     *
     *Used Mode:
     *  var MyAct = app.openMainAct();
     */
    this.openMainAct = function() {
        var a = document.createElement("script");
        a.src = this.getAppMainSource;
        document.body[0].appendChild(a);
                    //And HERE opens the main function!
    };

    //vars:
    this.getManifest = xhttp.responseXML.getElementsByTagName("Manifest")[0];
    this.getPackage = package;
    this.getAppFolder = "/@APP:/" + package;
    this.getAppMode = this.getManifest.getElementsByTagName("AppMode")[0];
    this.getName = xhttp.responseXML.getElementsByTagName("Manifest")[0].getElementsByTagName("AppName")[0];
    this.getJSource = this.getAppFolder + "/" + this.getManifest.getElentsByTagName("JSDir")[0];
    this.getJSDir = this.getManifest.getElementsByTagName("JSDir")[0];
    this.getVersion = this.getManifest.getElementsByTagName("Version")[0];
    this.getAppMainSource = this.getJSource + "/" + this.getManifest.getElementsByTagName("AppMain")[0] + ".js";
    this.getAppMain = this.getManifest.getElementsByTagName("AppMain")[0];
    this.getPermissionGroup = this.getManifest.getElementsByTagName("AppPerm")[0];
    this.getPermission = this.getPermissionGroup.getElementsByTagName("AddPerm");
}
};

但是main.js会调用所有函数!

如下所示,我们需要调用一个函数来启动一个应用程序:

var app = System.openApp("a.b.c");

并且必须存在一个同名的文件夹!

这是我的表现:

<?xml version="1.0" encoding="utf-8" ?>
<Manifest>
<JSDir>JS</JSDir>
<Version>0.0.1</Version>
<AppName>Documentação de API do EDOS</AppName>
<AppMode>windowed</AppMode>
<AppMain>MainAct</AppMain>
<AppPerm>
    <AddPerm>edos.permission.WRITE_APP</AddPerm>
    <AddPerm>edos.permission.READ_APP</AddPerm>
</AppPerm>
</Manifest>

对不起,如果我提供太多信息...... PS:我不想要API!

javascript html html5
1个回答
1
投票

导出文件底部的功能。并在文件顶部导入您的功能。

导入函数时,该函数将在该文件scope中可用,并可在导入时以您选择的名称访问。

如果将该函数导出为named

export main;

您可以使用别名导入它。

import { main as mainOne } from "./file1.js"
import { main as mainTwo } from "./file2.js"

// Call functions
mainOne();
mainTwo();

或者如果您将其导出为默认值(每个文件只能有一个默认导出)

export default main

然后你可以把它作为任何东西导入

import NewName from "./file1.js"

有关更多信息和示例,请查看导出here和导入here的文档。

© www.soinside.com 2019 - 2024. All rights reserved.