簡體   English   中英

JS屬性作為同一對象的方法的返回值

[英]JS property as return value from method of same object

編輯以包括有關我要實現的目標的更多信息。

下面的代碼工作正常,但是當我取消注釋boardGen:..board:..部分以使board的尺寸靈活時,則不會創建板,或者其尺寸為0(無錯誤)。 有人可以解釋一下原因,並告訴我如何使其正常工作嗎?

$(document).ready(function () {

    var app = {

        // define inital matrix
        // to do: build helper function to make arbitary size grid
        board : [[0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0]],

        // boardGen : function (rows, cols) {
            // var array = [],
            // row = [];
            // while (cols--)
                // row.push(0);
            // while (rows--)
                // array.push(row.slice());
            // return array;
        // },

        // board : function () {
            // return app.boardGen(6, 6);
        // },

        init : function () {
            // on startup
            app.createCells();
            app.main();
        },

        createCells : function () {
            // create cells and populate
            var html = '<table>';
            for (var i = 0, len = app.board.length; i < len; ++i) {
                html += '<tr>';
                for (var j = 0, rowLen = app.board[i].length; j < rowLen; ++j) {
                    html += `<td class="cell" data-gridpos="[${i}, ${j}]">`;
                    html += `${app.board[i][j]}</td>`;
                }
                html += "</tr>";
            }
            html += '</table>';
            $('#instructions').after(html);
        },

        numNeighbours : function (arr, coords) {
            var row = coords[0];
            var col = coords[1];
            var neighbours = 8;
            var offsets = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
            for (offset of offsets) {
                // prevent errors for non-existant indices
                try {
                    neighbour = arr[row + offset[0]][col + offset[1]];
                } catch (err) {
                    neighbour = undefined;
                }
                if (!neighbour) {
                    neighbours--;
                }
            }
            return neighbours;
        },

        main : function () {
            var coords = [];
            $(document).on('click', '.cell', function () {
                // get data for current cell
                coords = $(this).data('gridpos');
                // highlight current cell
                $(this).css('background-color', 'lightgray');
                $('td').not(this).css('background-color', '');
                // update info re neighbours
                $('#info').html('Touching: ' + app.numNeighbours(app.board, coords));
                // console.log(app.numNeighbours(coords));
                // toggle values in cells and update board
                if (app.board[coords[0]][coords[1]] == 1) {
                    app.board[coords[0]][coords[1]] = 0;
                    $(this).html('0')
                } else {
                    app.board[coords[0]][coords[1]] = 1;
                    $(this).html('1')
                }
            });
        }
    };
    app.init();
});
  1. 對象文字得到評估
  2. 創建一個對象
  3. 該對象已分配給app

您無法在第1步中讀取app的值,因為直到第3步它都沒有值。

暫無
暫無

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

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