【问题标题】:Unable to run simple example using library https://github.com/hiddentao/linear-algebra无法使用库 https://github.com/hiddentao/linear-algebra 运行简单示例
【发布时间】:2016-02-20 17:56:42
【问题描述】:

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

要使用的文档状态:

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() ref 吗?

感谢任何其他推荐的 js 数学库。

【问题讨论】:

    标签: javascript linear-algebra


    【解决方案1】:
    new linearAlgebra().Matrix(...)
    

    被解释为

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

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

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

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

    【讨论】:

    • new (linearAlgebra()).Matrix 也可以。或者您可以按照您的库的文档并清楚地导入符号,例如 var linAlg = linearAlgebra(), Vector = linAlg.Vector, Matrix = linAlg.Matrix; 有关示例,请参阅 this JSFiddle
    猜你喜欢
    • 1970-01-01
    • 2013-10-19
    • 2021-04-09
    • 2014-12-19
    • 2013-10-06
    • 2020-03-08
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    相关资源
    最近更新 更多