0

我使用构造函数方法在Javascript中定义了一个类Timeline,

function Timeline(formal parameters)
{
   //property definitions
}

这些方法是在原型对象上定义的,

Timeline.prototype.zoomMax = function(){ // Method Definition };

我正在使用数组来存储时间轴的对象,

var timeline = [];
timeline[0] = new Timeline(parameter's values);
timeline[1] = new Timeline(parameter's values);

但是当我调用 Timeline 的方法时,

timeline[0].zoomMax();

我收到错误,

未捕获的类型错误:无法读取未定义的属性“zoomMax”

注意:我已经检查了打印时间线 [0],对象正在存储在其中。

当我将对象存储在一个简单的变量而不是数组中时,它可以正常工作,

var timeline = new Timeline(parameter's values);

timeline.zoomMax(); //Gives me the output

我没有得到,如何对存储在数组中的对象调用方法 zoomMax()。

请指导。

我的时间线代码,

function Timeline(video_duration_in_sec,frames_per_sec = 25,zoom_value=1){

            ComponentContainer.call(this,'time-cover');
            this.video_duration_in_sec = video_duration_in_sec;
            this.frame_count = video_duration_in_sec * frames_per_sec;
            this.zoom_value = zoom_value;
            this.ruler_width = this.video_duration_in_sec * (100/this.zoom_value);
            this.min = 1;
            this.max = 25;                     


        } 
4

2 回答 2

1

使用class关键字定义对象类

class Timeline 
{
    constructor(parameters) {
        //property definitions, initialization...
    }

    zoomMax() {
        console.log('zoom');
    }
}


var timeline = [];
timeline[0] = new Timeline('parameters values');
timeline[1] = new Timeline('parameters values');

timeline[0].zoomMax();

于 2019-03-05T06:51:36.327 回答
0

这对我来说很好用

    function Timeline(params){ ... }

    Timeline.prototype.zoomMax = function() { ... } 

    var timeline = []
    timeline[0] = new Timeline(param1);
    timeline[1] = new Timeline(param2);

    timeline[0].zoomMax(); // Executes zoomMax function

您最初的错误可能是由于 zoomMax 方法的错误声明。有关此的更多信息在这里:https ://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Not_a_function

于 2019-03-05T07:08:41.797 回答