更新原型变量Javascript
我正在进行一个项目,需要通过方法更新原型变量Javascript,javascript,oop,prototype,Javascript,Oop,Prototype,我正在进行一个项目,需要通过方法video.prototype.getCurrentFrame()从播放的视频中返回帧数。除了此方法返回的数字为“未定义”之外,我的脚本工作得非常好。我知道我的问题与我的变量范围有关,但我是javascript新手,我似乎无法让它单独工作 在我的方法video.prototype.setUpPlayer中,我有一个函数,允许我计算framcount'timeListener',其中我更新了一个名为frame的变量; 如果我试图通过video.prototype.g
video.prototype.getCurrentFrame()
从播放的视频中返回帧数。除了此方法返回的数字为“未定义”之外,我的脚本工作得非常好。我知道我的问题与我的变量范围有关,但我是javascript新手,我似乎无法让它单独工作
在我的方法video.prototype.setUpPlayer
中,我有一个函数,允许我计算framcount'timeListener'
,其中我更新了一个名为frame的变量;
如果我试图通过video.prototype.getCurrentFrame()
访问此帧变量,它将不会达到更新后的值
以下是我目前的代码:
var Video = function(aVideoId){
this.videoId = aVideoId;
this.frame;
this.videoContainer;
this.myPlayer;
this.timeListener;
this.progressListener;
};
Video.prototype.getCurrentFrame = function(){
return this.frame;
}
Video.prototype.setVideoContainer = function(){
videoContainer = $('<div>', {
id: this.videoId,
class: 'projekktor',
width: "100%",
height: "100%",
});
$('#innerContainer').html(videoContainer);
}
Video.prototype.setUpPlayer = function(){
videoId = this.videoId;
myPlayer = projekktor('#' + videoId, {
controls: "true",
volume: 0.5,
preload: false,
autoplay: true,
playlist: [{
0: {
src: '/' + videoId + '.mp4',
type: 'video/mp4'
},
1: {
src: '/' + videoId + '.mov',
type: 'video/mov'
},
2: {
src: '/' + videoId + '.ogv',
type: 'video/ogv'
}
}]
}, function() { // call back
myPlayer.addListener('time', timeListener);
myPlayer.addListener('progress', progressListener);
});
timeListener = function(duration) {
$('#currentTime').html(duration);
frame = Math.round(duration * 25);
$('#currentFrame').html(frame);
return this.frame = frame;
}
progressListener = function(value) {
$('#progress').html(Math.round(value))
$('#progress2').html(myPlayer.getLoadProgress());
}
}
var Video=函数(aVideoId){
this.videoId=aVideoId;
这个框架;
这个视频容器;
这个.myPlayer;
这是timeListener;
这是我的听众;
};
Video.prototype.getCurrentFrame=函数(){
返回此.frame;
}
Video.prototype.setVideoContainer=函数(){
videoContainer=$(''){
身份证:这个,
类别:“projekktor”,
宽度:“100%”,
高度:“100%”,
});
$('#innerContainer').html(videoContainer);
}
Video.prototype.setUpPlayer=函数(){
videoId=this.videoId;
myPlayer=projekktor(“#”+videoId{
控件:“true”,
体积:0.5,,
预加载:false,
自动播放:对,
播放列表:[{
0: {
src:'/'+videoId+'.mp4',
类型:“视频/mp4”
},
1: {
src:'/'+videoId+'.mov',
类型:“视频/视频”
},
2: {
src:'/'+videoId+'.ogv',
键入:“视频/ogv”
}
}]
},函数(){//回调
myPlayer.addListener('time',timeListener);
myPlayer.addListener('progress',progressListener);
});
timeListener=函数(持续时间){
$('#currentTime').html(持续时间);
帧=数学轮(持续时间*25);
$('#currentFrame').html(frame);
返回this.frame=frame;
}
progressListener=函数(值){
$('#progress').html(Math.round(value))
$('#progress2').html(myPlayer.getLoadProgress());
}
}
提前感谢您的帮助 您需要从
Video
的实例调用getCurrentFrame
,而不是原型本身:
var video = new Video;
alert(video.getCurrentFrame());
使用原型检索当前帧的唯一方法是使用apply()
(这也需要一个实例):
编辑:似乎未在视频实例的上下文中执行
timeListener
回调。您可能必须将回调显式绑定到正确的范围:
timeListener = function()
{
// ...
this.frame = frame;
// ...
}
var video = new Video;
// binding the correct context
myPlayer.addListener('time', timeListener.bind(video));
这个
在timeListener中
闭包现在是视频
你是如何调用函数的。getCurrentFrame()
你在你的codeOne注释中没有显示任何视频
的实例:我建议你指定你在函数中使用的变量的范围。它要么是私有的(通过添加var
,要么添加this
,使其在原型中可访问)。否则,您将在全局范围内创建大量“垃圾”。不过,此注释与您的问题无关。谢谢,我已经使用了一个实例来使用函数getCurrentFrame()。也许我应该更准确地描述这一点。但我的问题是this.frame在setUpPlayer方法之外不会更改。因此,当我实例化我的视频对象并将其称为getCurrentFrame时。该方法首先返回已放入构造函数中的值(在我的示例中为nothing)。当我希望它返回更新的值时
timeListener = function()
{
// ...
this.frame = frame;
// ...
}
var video = new Video;
// binding the correct context
myPlayer.addListener('time', timeListener.bind(video));