在javascript中创建矩阵数据类

时间:2013-08-27 18:45:01

标签: javascript matrix

我正在尝试创建一个存储数据值的矩阵。

这是我到目前为止所做的:

var Matrix = new function(rows, columns) {
var matrix = [[]];
var i,j;
this.matrix[i].length == columns;
this.matrix[i][j].length == rows;
if(matrix[i] === undefined){
    matrix[i] == 0;
}
}

Matrix.prototype = {
addValue: function (i,j,value) {
this.matrix[i][j].push(value);
console.log(this);
}
}

var m = new Matrix ();

m.addValue(1,1,"this is where I place it");
console.log(m);

我无法摆脱这个错误:

TypeError:无法读取undefined

的属性'undefined'

有任何建议或更正吗?

1 个答案:

答案 0 :(得分:0)

一个非常简单的矩阵可能是:

Matrix = function() {
    this.rows = [];
}

Matrix.prototype = {

    put : function (row, column, value) {
        if (this.rows[row] === undefined) {
            this.rows[row] = [];
        }

        var row = this.rows[row];

        row[column] = value;

    },

    get : function (row, column) {
        return this.rows[row][column];
    }

}