JS文件未正确加载

时间:2016-12-20 20:20:51

标签: javascript

我遇到的问题是,我认为文件rules.jsactions.js (位于categories文件夹中)的文件并未正确加载

的index.html:

<script type="text/javascript" src="categories/rules.js"></script>
<script type="text/javascript" src="categories/actions.js"></script>
<script type="text/javascript" src="quando_blocks.js"></script>
<script type="text/javascript" src="quando_editor.js"></script>
<script type="text/javascript" src="js/index.js"></script>

quando_editor:

var quando_blocks = this["quando_blocks"];

quando_editor.js (或与index.html处于同一级别的任何文件)中,this["**filename**"]可用于将文件保存到变量中。然后,您就可以调用该文件中的函数。

尝试对rule.js this["rules"]执行相同操作时,它返回undefined (我认为同样适用于actions。注销this,我可以看到文件quando_blocksquando_editor已正确加载但不是rulesactions

enter image description here

在DevTools中检查网络,我可以看到实际加载了rules.jsactions.js,就在我使用this引用它们时,它们不存在。

此外,index.js也可以正常加载。

这是我的文件结构

editor
 |
 +- categories
    |
    +- rules.js
    +- action.js
 |
 +- js
    |
    +- index.js
 |
 +- index.html
 +- quando_blocks.js
 +- quando_editor.js

注意:

  • 文件所处的级别似乎没有什么区别
  • <script>代码的顺序也没有区别
  • 我还尝试了this["categories/rules"],它还会返回undefined

更新

我现在可以在窗口对象中看到我的rulesactions个对象:

enter image description here

但是,当尝试访问这些对象上的方法时,它会抛出:

  

无法读取未定义的属性“addBlocks”

我以为我已经修好了但显然不是......

quando_blocks.js (相关部分)

(() => {
    let self = this["quando_blocks"] = {};

    let rules = this["rules"];
    let actions = this["actions"];
    let ropedClimbing = this["ropedClimbing"];

    self.addBlocks = (quando_editor) => {
        rules.addBlocks(quando_editor);
        actions.addBlocks(quando_editor);
        ropedClimbing.addBlocks(quando_editor);
    }
})();

rules.js (相关部分)

(() => {
    let self = this["rules"] = {};

    self.addBlocks = (quando_editor) => {
        ...
    };
})();

0 个答案:

没有答案
相关问题