I am creating a matrix class (which will be used in the reaction application), which I would like to define as follows:
class Matrix extends Object {
constructor(r, c) {
super();
this.rows = new Array(r);
for (var i=0; i<r; i++) {
this.rows[i] = new Array(c);
}
this.assign((v,i,j) => (i===j)? 1:0);
}
assign(f) {
for (var i=0; i<this.rows.length; i++) {
for (var j=0; j<this.rows[i].length; j++) {
this.rows[i][j] = f(this.rows[i][j], i, j);
}
}
}
This compiles through webpack, but when I work, I get an error on the Chrome console saying that _this.assign is not a function .
I can get it to work as follows:
constructor(r, c) {
super();
this.rows = new Array(r);
for (var i=0; i<r; i++) {
this.rows[i] = new Array(c);
}
this.assign = function(f) {
for (var i=0; i<r; i++) {
for (var j=0; j<r; j++) {
this.rows[i][j] = f(this.rows[i][j], i, j);
}
}
};
this.assign((v,i,j) => (i===j)? 1:0);
}
But this is wrong, right? I would not need to define all the functions of the object in the constructor, right? In the same file, I define reaction classes, for example:
class MatrixComponent extends React.Component { ... }
and they can call functions without defining them in the constructor. What am I missing?
source
share