Javascript原型:如何在另一个方法中调用一个方法
Javascript prototype: how do I call a method within another method?
这是我第一次尝试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;
}
同意研究this
、Function.prototype.bind
,以及一般的原型链。此外,javascript是一种动态绑定的语言。您可以重新绑定this
。
您可以将对此的引用存储为闭包变量,也可以将要传递给setTimeout
的函数文本绑定到所需的上下文(在该上下文中Sensor.prototype
或this
)
编辑:代替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()
}
相关文章:
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- javascript从子方法调用父方法
- 我应该返回一个类似console.log()的方法调用吗
- NodeJS-从同一文件中的另一个方法调用一个方法
- 使用restangular save方法调用put与post时的控件
- 从实例方法调用实例方法
- Node.js-异步方法调用问题
- Jquery事件绑定获胜'当作为方法调用时不起作用,但当直接在控制台中调用时会起作用
- 向模板实例变量传递调用方法调用的结果时出现异常
- jQuery 方法调用了两次
- 不能从 Backbone.View 的方法调用 Backbone.collection 的方法,其中包含 require
- 组合 onLayout 的方法调用
- lodash/下划线 - 使用绑定与方法调用和方法保持未绑定
- 从公共方法调用模块的私有函数
- 如何对 jqGrid 的 loadComplete 和 onSelectRow 进行基因预和后方法调用
- 完全限定的 JavaScript 方法调用
- 为什么我在 Angular JS 中收到两个方法调用
- 如何在从排序方法调用参数时将其传递给回调
- 使用两种不同的方法调用方法
- 如何在 javascript 中更改方法调用的全局范围