Javascript原型:如何在另一个方法中调用一个方法

Javascript prototype: how do I call a method within another method?

本文关键字:方法 调用 一个 Javascript 另一个 原型      更新时间:2023-09-26

这是我第一次尝试javascript原型设计。 我正在使用Espruino(微控制器的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();

在你的代码中,回调中的"this"setTimeout 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()
  }

声明全局方法foo和子方法bar并将子方法本身称为函数,可以通过寻址之前声明的变量在子方法中使用this关键字,然后将返回变量声明为子方法进程。

Foo: function()
{
    var a='a';
    var ret='b';
    var Bar = function()
    {
        this.a='a';
        return 'a';    
    }
    ret=Bar();
    return ret; 
}

同意研究thisFunction.prototype.bind,以及一般的原型链。此外,javascript是一种动态绑定的语言。您可以重新绑定this

您可以将对此的引用存储为闭包变量,也可以将要传递给setTimeout的函数文本绑定到所需的上下文(在该上下文中Sensor.prototypethis

编辑:代替bind可用,您可以通过应用,调用或关闭来完成同样的事情。

getSensorResult:function () {
    a = this.getSensorAddress;
    c = this.cmdTable.Reading.R.cmd;
    w = this.cmdTable.Reading.R.wait;
    //I2C1.writeTo(a, c);
    setTimeout((function(_this) {
      return function (e) {
        _this.getSensorReading();
      }
    })(this), w); //Attempting to execute getSensorReading()
}