简体   繁体   English

prototype.Function没有在node.js中导出

[英]prototype.Function is not being exported in node.js

I found this javascript Kalman filter library online and I wanted to use it with my node.js application. 我在线上找到了这个javascript Kalman过滤器库 ,我想将其与我的node.js应用程序一起使用。 As I wanted to include this js file into my node.js application, I tried to export the required functions ( added module.exports as shown below) . 当我想将此js文件包含到我的node.js应用程序中时,我尝试导出所需的功能(如下所示添加了module.exports)。

module.exports = {

  KalmanModel : function(){

    function KalmanModel(x_0,P_0,F_k,Q_k){
      this.x_k  = x_0;
      this.P_k  = P_0;
      this.F_k  = F_k;
      this.Q_k  = Q_k;
    }

    KalmanModel.prototype.update =  function(o){
      // code 
    }

    return KalmanModel;
  },

  KalmanObservation : function(){

    function KalmanObservation(z_k,H_k,Q_k){
      this.z_k = z_k;//observation
      this.H_k = H_k;//observation model
      this.R_k = R_k;//observation noise covariance
    }

    return KalmanObservation;
  }
};

But when I try to run the following piece of code 但是当我尝试运行以下代码时

Code: 码:

var kal =require('./kalman');
var observationVal = observationValues;
var x_0 = $V([observationVal[0]]);
var P_0 = $M([[2]]);
var F_k=$M([[1]]);
//process noise
var Q_k=$M([[0]]);
var KM = kal.KalmanModel(x_0,P_0,F_k,Q_k);
//value we measure
var z_k = $V([observationVal[0]]);
var H_k = $M([[1]]);
var R_k = $M([[4.482]]);
var KO = kal.KalmanObservation(z_k,H_k,R_k);

for (var i=1;i<10;i++){
z_k = $V([observationVal[i]]);
KO.z_k=z_k;
KM.update(KO);

I get an error 我得到一个错误

TypeError: object is not a function. TypeError:对象不是函数。

It looks like method defined with prototype is not getting exported. 看来没有用原型定义的方法被导出。 What am I doing wrong here? 我在这里做错了什么?

I'm really struggling to understand why the top piece of code is written as it is (with named properties matching the same names as the function within) 我真的很难理解为什么按原样编写顶部代码(命名属性与其中的函数名称相同)

My suggestions would be log out some variables to see what their value is. 我的建议是注销一些变量以查看其值。

Namely: kal , typeof kal , typeof kal.KalmanModel . 即: kaltypeof kaltypeof kal.KalmanModel Also, i think you need to create an instance of KalmanModel to use the prototype functions, so try: 另外,我认为您需要创建KalmanModel的实例才能使用原型函数,因此请尝试:

var KM = new kal.KalmanModel(x_0,P_0,F_k,Q_k);

Well if you don't wanna change much in the existing source code you can do this. 好吧,如果您不想对现有源代码进行太多更改,则可以执行此操作。 Don't remove the implicit function call ie : (function(){//code})(); 不要删除隐式函数调用,即: (function(){//code})(); and i think you should be set. 我认为你应该被设置。

module.exports = {

    KalmanModel : (function(){

        function KalmanModel(x_0,P_0,F_k,Q_k){
          this.x_k  = x_0;
          this.P_k  = P_0;
          this.F_k  = F_k;
          this.Q_k  = Q_k;
        }

        KalmanModel.prototype.update =  function(o){
          // code 
        }

        return KalmanModel;
    })(),

    KalmanObservation : (function(){

        function KalmanObservation(z_k,H_k,Q_k){
          this.z_k = z_k;//observation
          this.H_k = H_k;//observation model
          this.R_k = R_k;//observation noise covariance
        }

        return KalmanObservation;
    })()
};

Or simply append the following in the library kalman.js 或者只是将以下内容添加到库kalman.js中

module.exports = {
    myKalmanModel : KalmanModel,
    myKalmanObservation : KalmanObservation
}

and access as 和访问为

var KM = kal.myKalmanModel(x_0,P_0,F_k,Q_k);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM