【发布时间】:2017-05-22 09:59:30
【问题描述】:
只是一个简单的问题。
我想将HTMLMediaElement 方法分配给变量。
// html part
<video id="player" ... />
// js part
const video = document.querySelector('#player')
const play = video.play
video.play() // works!
play() // error!
Uncaught (in promise) TypeError: Failed to execute 'play' on 'HTMLMediaElement': Illegal invocation
有人知道为什么会发生这个错误吗?
【问题讨论】:
-
我认为您需要将
this绑定到video? --- 是的,绑定工作,play = play.bind(video) -
这个问题感觉像是重复的……但是我找不到重复的目标。
-
如果您没有找到上一个问题。您可以将答案发布到答案块。
-
但我只是想知道是否有任何函数式编程方法可以在没有绑定的情况下解决它?
-
函数式编程方式?
Bind是函数式编程...
标签: javascript html html5-video