无法使用库https://github.com/hiddentao/linear-algebra运行简单示例

时间:2016-02-20 17:56:43

标签: javascript linear-algebra

我正在尝试使用库:https://github.com/hiddentao/linear-algebra

Doc说明要使用:

Include dist/linear-algebra.js script into your HTML.

In the browser the library is exposed via the linearAlgebra() function.

但是使用代码:

<script src="linear-algebra.js"></script>

<!-- https://github.com/hiddentao/linear-algebra
 -->

<script>

var m = new linearAlgebra().Matrix([ [1, 2, 3], [4, 5, 6] ]);
console.log( m.rows );     // 2
console.log( m.cols );     // 3
console.log( m.data );     // [ [1, 2, 3], [4, 5, 6] ]

</script>

导致Chrome错误:

Uncaught TypeError: Cannot read property 'rows' of undefined

我没有以正确的方式使用该库,应该只能使用linearAlgebra()参考?

任何其他建议js数学图书馆赞赏。

1 个答案:

答案 0 :(得分:1)

new linearAlgebra().Matrix(...)

被解释为

( new linearAlgebra() ).Matrix(...)

由于JS优先规则(详见here)。

将其括在括号中以获得您想要的内容:

new ( linearAlgebra().Matrix )(...)