Typescript 找不到函数名
我正试图做我的第一个TypeScript/React项目,但我遇到了一些问题 通过使用,我能够读取和播放麦克风中的声音,并在控制台中显示一些样本分析数据。现在我试着将其转化为TS。一步一步地,我得出了以下结论:Typescript 找不到函数名,typescript,Typescript,我正试图做我的第一个TypeScript/React项目,但我遇到了一些问题 通过使用,我能够读取和播放麦克风中的声音,并在控制台中显示一些样本分析数据。现在我试着将其转化为TS。一步一步地,我得出了以下结论: export class Processor { readonly BUFFER_SIZE = 16384; audioContext: AudioContext; gainNode: GainNode; microphoneStream: MediaE
export class Processor {
readonly BUFFER_SIZE = 16384;
audioContext: AudioContext;
gainNode: GainNode;
microphoneStream: MediaElementAudioSourceNode;
constructor() {
this.audioContext = new AudioContext();
console.log('audio is starting up ...');
if (navigator.getUserMedia) {
navigator.getUserMedia(
{ audio: true },
function (stream) {
startMicrophone(stream);
},
function (e) {
alert('Error capturing audio.');
});
} else {
alert('Seems like this browser might not be supported.');
}
}
private startMicrophone(stream: MediaStream) {
this.gainNode = this.audioContext.createGain();
this.gainNode.connect(this.audioContext.destination);
this.microphoneStream =
this.audioContext.createMediaStreamSource(stream);
}
}
除了给startMicrophone打电话给我
'Cannot find name 'startMicrophone'.'
我还试图用这个引用它,这导致了一个不同的错误:
''this' implicitly has type 'any' because it does not have a type annotation.'
我不知道我做错了什么,可能真的需要一些指导。建议:如果你想使用这个函数,你必须使用箭头函数,因为如果你在函数块中写入这个函数,它引用的是当前函数,而不是父函数
export class Processor {
readonly BUFFER_SIZE = 16384;
audioContext: AudioContext;
gainNode: GainNode;
microphoneStream: MediaElementAudioSourceNode;
constructor() {
this.audioContext = new AudioContext();
console.log('audio is starting up ...');
if (navigator.getUserMedia) {
navigator.getUserMedia({
audio: true
},
(stream) => {
this.startMicrophone(stream);
},
(e) => {
alert('Error capturing audio.');
});
} else {
alert('Seems like this browser might not be supported.');
}
}
private startMicrophone(stream: MediaStream) {
this.gainNode = this.audioContext.createGain();
this.gainNode.connect(this.audioContext.destination);
this.microphoneStream =
this.audioContext.createMediaStreamSource(stream);
}
}
另一种方法是,您可以将其分配给其他变量,并使用const self=this;在函数内部使用self
constructor() {
const self = this;
this.audioContext = new AudioContext();
if (navigator.getUserMedia) {
navigator.getUserMedia({
audio: true
},
function (stream) {
self.startMicrophone(stream);
},
function (e) {
alert('Error capturing audio.');
});
} else {
alert('Seems like this browser might not be supported.');
}
}
建议:如果要使用此函数,则必须使用箭头函数,因为如果在函数块中写入此函数,它将引用当前函数,而不是父函数
export class Processor {
readonly BUFFER_SIZE = 16384;
audioContext: AudioContext;
gainNode: GainNode;
microphoneStream: MediaElementAudioSourceNode;
constructor() {
this.audioContext = new AudioContext();
console.log('audio is starting up ...');
if (navigator.getUserMedia) {
navigator.getUserMedia({
audio: true
},
(stream) => {
this.startMicrophone(stream);
},
(e) => {
alert('Error capturing audio.');
});
} else {
alert('Seems like this browser might not be supported.');
}
}
private startMicrophone(stream: MediaStream) {
this.gainNode = this.audioContext.createGain();
this.gainNode.connect(this.audioContext.destination);
this.microphoneStream =
this.audioContext.createMediaStreamSource(stream);
}
}
另一种方法是,您可以将其分配给其他变量,并使用const self=this;在函数内部使用self
constructor() {
const self = this;
this.audioContext = new AudioContext();
if (navigator.getUserMedia) {
navigator.getUserMedia({
audio: true
},
function (stream) {
self.startMicrophone(stream);
},
function (e) {
alert('Error capturing audio.');
});
} else {
alert('Seems like this browser might not be supported.');
}
}
非常感谢,不仅它起作用了,而且你还解释了它为什么起作用。我会在一两个小时内避免接受,以防有人提供更详细的答案。谢谢@Lasooch很好,检查我的更新答案,你会对此有更清楚的了解。self=这是ES6中的反模式。因为我们有箭头,正是为了这个目的。箭头函数是官方名称,还是仅仅是lambda函数的通俗说法?@Lasooch,这是官方名称“胖箭”是一种口语。非常感谢,它不仅起作用了,而且你还解释了它为什么起作用。我会在一两个小时内避免接受,以防有人提供更详细的答案。谢谢@Lasooch很好,检查我的更新答案,你会对此有更清楚的了解。self=这是ES6中的反模式。因为我们有箭头,正是为了这个目的。箭头函数是官方名称,还是仅仅是lambda函数的通俗说法?@Lasooch,这是官方名称“胖箭”是口语。