JavaScript原型(prototype): how do I call a method within another method?

标签 javascript prototype

这是我第一次尝试 JavaScript 原型(prototype)设计。我正在使用 Espruino(微 Controller 的 JavaScript 解释器)从环境传感器获取数据。我试图将代码暂停 1000 毫秒,然后执行 getSensorReading() 方法,该方法(我认为)位于另一个方法内部。如何执行 getSensorReading() 方法?我相信错误是由以下原因引起的:

setTimeout(function (e) { this.getSensorReading(); }, w); //Attempting to execute getSensorReading()

我收到此错误:

Uncaught Error: Function "getSensorReading" not found!  at line 1 col 8 { this.getSensorReading(); }
        ^ in function called from system

代码:

I2C1.setup({scl:b6, sda:b7});

function Sensor (theType, theAddress) {
  this.type = theType;   //i.e. PH
  this.address = theAddress;  //i2c Address
  this.sensorResult = 0; //Store sensor result
  this.cmdTable = {
    "Calibrate" : {  //List of Calibration commands and timeout value.
      "clear" : { "cmd" : "Cal,Clear",      "wait" : 300  },
      "mid"   : { "cmd" : "Cal,mid,7.00",   "wait" : 1300 },
      "low"   : { "cmd" : "Cal,low,4.00",   "wait" : 1300 },
      "high"  : { "cmd" : "Cal,high,10.00", "wait" : 1300 },
      "query" : { "cmd" : "Cal,?",          "wait" : 300  }
    },
    "Information" : {  //Device Information

    },
    "LED" : {  //Enable / Disable or Query the LEDs
      "L0" : { "cmd" : "L,0", "wait" : 300 },
      "L1" : { "cmd" : "L,1", "wait" : 300 },
      "L?" : { "cmd" : "L,?", "wait" : 300 }
    },
    "Reading" : {  //Takes a single reading
      "R" : { "cmd" : "R,25.6", "wait" : 1000 } //Takes a single temperature compensated reading
    },
    "Serial"      : {  //Switch back to UART mode

    },
    "Sleep"       : {  //Enter low power sleep mode

    },
    "Status"      : {  //Retrieve status information

    },
    "Temperature" : {  //Set or Query the temperature compensation
      "T"  : { "cmd" : "T,19.5", "wait" : 300 },  //Where the temperature is any value; floating point, or int, in ASCII form
      "T?" : { "cmd" : "T,?",   "wait" : 300 }  //Query the set temerature
    },
    "Factory"     : {  //Factory reset

    },
  };
}

Sensor.prototype = {
  constructor: Sensor,
  getSensorType:function () {
    return this.type; //Get Sensor type
  },
  getSensorAddress:function () {
    return this.address; //Get Sensor address
  },
  getSensorReading:function() {
    a = this.getSensorAddress;
    console.log("i2c Address: " + a);
    //d = I2C1.readFrom(a, 7);
    return d;
  },
  getSensorResult:function () {
    a = this.getSensorAddress;
    c = this.cmdTable.Reading.R.cmd;
    w = this.cmdTable.Reading.R.wait;

    //I2C1.writeTo(a, c);
    setTimeout(function (e) { this.getSensorReading(); }, w); //Attempting to execute getSensorReading()
  },
  storeSensorResult:function () {

  },
  updateResTemp:function (temp) {
    console.log("Before: " + this.cmdTable.Reading.R.cmd);
    this.cmdTable.Reading.R.cmd = "R," + temp;
    console.log("After: " + this.cmdTable.Reading.R.cmd);
  }
};

var ph = new Sensor("ph", 0x63);

ph.updateResTemp(90.5);
ph.getSensorResult();

最佳答案

在代码中,setTimeout 回调中的“this”引用父函数getSensorResult。该函数没有名为 getSensorReading 的函数。这就是错误的原因...此外,当您在函数内使用全局变量时,可能会因变量出价过高而出现意外结果。使用 var 关键字使它们全局化。 尝试像这样改变..

getSensorReading:function() {
    var a = this.getSensorAddress;
    console.log("i2c Address: " + a);
    //var d = I2C1.readFrom(a, 7);
    return d;
  },
getSensorResult:function () {
    var a = this.getSensorAddress;
    var c = this.cmdTable.Reading.R.cmd;
    var w = this.cmdTable.Reading.R.wait;
    var that = this;
    //I2C1.writeTo(a, c);
    setTimeout(function (e) { that.getSensorReading(); }, w); //Attempting to execute getSensorReading()
  }

关于JavaScript原型(prototype): how do I call a method within another method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27009400/

相关文章:

javascript - 在 ASP.NET Core MVC 应用程序中的 Controller 和 JavaScript 之间传递参数?

javascript - 如何使用通量 slider 更改图像大小

javascript - 绳索模拟html5

javascript - 使用 String.prototype 扩展 String 时无法覆盖 javascript 中的 "this"

javascript - 将相同函数分配给 JavaScript 中的多个变量时的性能问题

javascript - 双字节整数转单字节-Javascript

javascript - 使用 src img 将 Attr 设置为最接近的 <a>

javascript - 调用父类中被重写的函数

javascript - 在 Javascript Prototype 中使用定时器循环

javascript - 通过 Object.defineProperty 添加原型(prototype)属性覆盖构造函数属性