HTML语音不适用于Safari mac"TypeError"

Jam*_*mes 5 javascript safari html5 text-to-speech

在mac 10.0.1上,HTML5语音无法在Safari上运行,

我收到错误,

TypeError:语音1('话语')到SpeechSynthesis.speak必须是SpeechSynthesisUtterance的一个实例

它适用于Chrome和Firefox,我很确定它曾用于Safari ...

var u = new SpeechSynthesisUtterance();
u.text = "hello world";
u.lang = "en";
window.speechSynthesis.speak(u);
Run Code Online (Sandbox Code Playgroud)

Jam*_*mes 2

好吧,终于想通了。

我有一些兼容性代码来支持没有 html5 语音的浏览器,

if (SpeechSynthesisUtterance == undefined) {
    function SpeechSynthesisUtterance(text) {
        this.text = text;
    }
}
Run Code Online (Sandbox Code Playgroud)

这适用于 Chrome 和 Firefox,但在 Safari 上,似乎任何脚本中的任何函数都会在解析脚本时进行评估,因此即使 SpeechSynthesisUtterance 已经存在,该函数也会被声明。

我想我需要以不同的方式做这件事......