简体   繁体   English

JS属性作为同一对象的方法的返回值

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

Edited to include more info about what I'm trying to achieve. 编辑以包括有关我要实现的目标的更多信息。

The code below works fine, but when I uncomment the boardGen:.. and board:.. sections to make board of flexible size, no board is created, or it's of 0 dimensions (no error). 下面的代码工作正常,但是当我取消注释boardGen:..board:..部分以使board的尺寸灵活时,则不会创建板,或者其尺寸为0(无错误)。 Could someone please explain why, and tell me how to make it work properly? 有人可以解释一下原因,并告诉我如何使其正常工作吗?

$(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. The object literal gets evaluated 对象文字得到评估
  2. An object is created 创建一个对象
  3. The object is assigned to app 该对象已分配给app

You can't read the value of app during step 1, since it doesn't have a value until step 3. 您无法在第1步中读取app的值,因为直到第3步它都没有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM