1

在我正在使用网络摄像头的应用程序中。要访问它,我使用了 webcam.js ( https://pixlcore.com/ )。但是当我在 Eclipse 中打开它时,它显示错误为:Syntax error on token "catch", Identifier expected. 小代码片段:

            var self = this;
            this.mediaDevices.getUserMedia({
                "audio": false,
                "video": this.params.constraints || {
                    mandatory: {
                        minWidth: this.params.dest_width,
                        minHeight: this.params.dest_height
                    }
                }
            })
            .then( function(stream) {
                // got access, attach stream to video
                video.src = window.URL.createObjectURL( stream ) || stream;
                self.stream = stream;
                self.loaded = true;
                self.live = true;
                self.dispatch('load');
                self.dispatch('live');
                self.flip();
            })
            .catch( function(err) {  //here shows error
                return self.dispatch('error', "Could not access webcam: " + err.name + ": " + err.message, err);
            });

是什么原因以及如何解决?

4

1 回答 1

3

问题显然catch是一个保留关键字,因此您的代码检查器认为这是一个错误。但是,您的代码检查器实际上是错误的,并且catch也是有效的方法调用。也就是说,除非你是旧版本的 IE。

在旧版本的 IE 中,此代码将失败,因为它也存在假设/catch外部无效的问题。我相信这个问题在 IE9 或 IE10 中都已修复,不确定。trycatch

catch无论如何,您可以通过在具有括号属性访问权限的字符串中使用旧 IE 和其他具有此一般问题的东西来解决此问题:

// ...
.then( function(stream) {
    // ...
})
['catch']( function(err) { 
    // ...
});
于 2016-06-02T04:57:19.517 回答