首页 > 解决方案 > 我在 Matrix 元素构造函数中输入什么来填充矩阵?

问题描述

    export class Matrix {

    constructor(...m) {
        console.log(...m);
        this.m = new Array(16).fill(0).splice(0, m.length, ...m);
    }

    toArray() {
        return [...this.m];
    }

    toString() {
        return `(${this.m.join(',')})`;
    }
    negate(){

    }
}
const mtrx = new Matrix(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);

这是老师给我的构造函数,怎么调用这个构造函数,把元素放进去呢?...m 表示任何数组,但我无法在线找到正确的语法。如果我称它为我是如何得到一个充满 0 的矩阵的。

标签: javascript

解决方案


有效的只是简单地这样做:

this.m = new Array(16).fill(0);
this.m.splice(0, m.length, ...m);


推荐阅读