首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新原型变量Javascript

更新原型变量Javascript
EN

Stack Overflow用户
提问于 2012-07-04 19:03:16
回答 1查看 256关注 0票数 0

我正在做一个项目,在这个项目中,我需要通过video.prototype.getCurrentFrame()方法从播放的视频中返回帧计数。我的脚本运行得很好,除了这个方法返回的数字是“未定义的”。我知道我的问题与我的变量作用域有关,但是我是一个新的javascript,而且我似乎不能让它独立工作……

在我的方法video.prototype.setUpPlayer中,我有一个函数允许我计算framcount帧,其中我更新了一个名为'timeListener'的变量;如果我试图通过video.prototype.getCurrentFrame()访问这个frame变量,它不会达到更新值。

到目前为止,我的代码如下:

代码语言:javascript
复制
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());
        }   
}

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2012-07-04 19:08:33

您需要从Video的实例调用getCurrentFrame,而不是从原型本身调用:

代码语言:javascript
复制
var video = new Video;
alert(video.getCurrentFrame());

使用原型检索当前帧的唯一方法是使用apply() (这也需要一个实例):

代码语言:javascript
复制
var video = new Video;
alert(Video.prototype.getCurrentFrame.apply(video));

EDIT:似乎timeListener回调不是在视频实例的上下文中执行的。您可能需要显式地将回调绑定到正确的作用域:

代码语言:javascript
复制
timeListener = function() 
    {
    //  ...
        this.frame = frame;
    //  ...
    }

var video = new Video;

// binding the correct context
myPlayer.addListener('time', timeListener.bind(video));

timeListener闭包中的this现在是video

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11327804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档