这是我第一次尝试javascript原型设计。我使用Espruino(微控制器的javascript解释器)从环境传感器获取数据。我试图暂停我的代码1000ms,然后执行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();
答案 0 :(得分:2)
在您的代码中,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()
}
答案 1 :(得分:1)
声明全局方法foo
和子方法bar
并将子方法称为函数本身,您可以通过寻址先前声明的变量在子方法中使用this
关键字,然后将return变量声明为子方法进程。
Foo: function()
{
var a='a';
var ret='b';
var Bar = function()
{
this.a='a';
return 'a';
}
ret=Bar();
return ret;
}
答案 2 :(得分:0)
同意调查this
,Function.prototype.bind
以及一般的原型链。此外,javascript是一种动态绑定语言。您可以重新绑定this
。
您可以将对此的引用存储为闭包变量,也可以将传递给setTimeout
的函数文字绑定到所需的上下文(Sensor.prototype
或this
in那个背景)
编辑:代替让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()
}