簡體   English   中英

JavaScript中匿名函數之間的通信

[英]Communication between anonymous functions in JavaScript

我本可以將所有代碼收集在一個文件中,然后將所有內容放在一個匿名函數中,但是我想知道是否有可能將它們分成單獨的文件,並讓它們彼此通信。 這是一個例子。 由於所有內容都在匿名函數中,因此我必須使用window對象將其“取出”,以便robot.html可以訪問這些對象。 當第一個文件嘗試通過方法訪問第二個文件時,會發生此問題:

Maze.prototype.setWall = function (x, y, direciton) {
    this.spaces[x][y].setWall(direciton);
};

我應該如何解決這個問題?

文件1:maze.js

(function (window) {
    'use strict';

    function Maze (width, height) {
        this.width = width;
        this.height = height;

        this.startX             = null;
        this.startY             = null;
        this.startOrientation   = null;
        this.endX               = null;
        this.endY               = null;

        this.spaces = [];

        var x, y;
        for (x = 1; x <= width; x += 1) {
            this.spaces[x] = [];
            for (y = 1; y <= height; y += 1) {
                this.spaces[x][y] = new MazeSpace();
            }
        }
    }

    Maze.prototype.setStart = function (x, y, orientation) {
        this.startX = x;
        this.startY = y;
        this.startOrientation = orientation;
    };

    Maze.prototype.setEnd = function (x, y) {
        this.endX = x;
        this.endY = y;
    };

    Maze.prototypes.setWall = function (x, y, direciton) {
        this.spaces[x][y].setWall(direciton);
    };

    window.Maze = Maze;

})(window);

文件2:mazespace.js

(function (window) {
    'use strict';

    function MazeSpace () {
        this.north = false;
        this.east = false;
        this.south = false;
        this.west = false;
    }

    MazeSpace.prototype.setWall = function (direction) {
        this[direction] = true;
    };

    window.MazeSpace = MazeSpace;

})(window);

文件3:robot.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>oojs</title>

    <script type="text/javascript" src="maze.js"></script>
    <script type="text/javascript" src="mazespace.js"></script>

    <script>
        var m = new Maze(7, 5);
        m.setStart(1, 1, 'north');
        m.setEnd(7, 1);
        m.setWall(1, 1, 'east');
    </script>

</head>
<body>
</body>
</html>

控制台輸出以下錯誤:

Uncaught TypeError:無法設置未定義maze.js的屬性“ setWall”:36 Uncaught ReferenceError:未定義迷宮

編輯:

由於錯別字而發生錯誤。

通常有兩個答案:

  1. 使用作為對象的單個全局變量,並使該“模塊”對象被該對象的屬性引用(或使您的函數對該對象的屬性)

  2. 使用像RequireJS這樣的AMD庫(這也涉及一個全局庫-RequireJS庫本身,但是如果您的依賴項非常復雜,則可能會很有用)

例如,#1可能看起來像這樣:

mazespace.js:

var Amazing = Amazing || {};
Amazing.MazeSpace = (function() {
    // ...

    return MazeSpace;
})();

maze.js:

var Amazing = Amazing || {};
Amazing.Maze = (function() {
    // ...

    return Maze;
})();

然后使用Amazing.MazeSpaceAmazing.Maze

也許更簡單的解決方案是修復maze.js中的錯字。 代替:

Maze.prototypes.setWall = function (x, y, direciton) {
    this.spaces[x][y].setWall(direciton);
};

它應顯示為:

Maze.prototype.setWall = function (x, y, direction) {
    this.spaces[x][y].setWall(direction);
};

您在prototype的末尾有一個額外的s ,因此該函數在以后的調用中是未知的(我還修復了方向上的拼寫錯誤:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM