我是Java面向对象编程的新手,我正在尝试为从API调用返回的项目列表构建处理程序对象和库。理想情况下,我希望库函数成为处理程序类的成员。但是,我无法让我的类方法正常工作。我将方法getModifiedDateTime定义为类bcObject的一部分,但是当我尝试回显此方法的对象调用结果时,出现此错误:

Error on line 44 position 26: Expected ';'
    this.getModifiedDateTime: function(epochtime) {

这使我相信我的方法定义只是语法上的问题,但我不知道在哪里。
response(
    {
        "items":
        [
            {"id":711,"name":"Shuttle","lastModifiedDate":"1268426336727"},
            {"id":754,"name":"Formula1","lastModifiedDate":"1270121717721"}
        ],
        "extraListItemsAttr1":"blah",
        "extraListItemsAttr2":"blah2"
    });

function response(MyObject) {
    bcObject = new bcObject(MyObject);

    thing = bcObject.getModifiedDateTime(bcObject.videoItem[0].lastModifiedDate);
    SOSE.Echo(thing);
}

function bcObject(listObject) {
  // define class members
  this.responseList = {};
  this.videoCount = 0;
    this.videoItem = [];
    this.responseListError = "";

    // instantiate members
    this.responseList = listObject;
    this.videoCount = listObject.items.length;

    // populate videoItem array
    for (i=0;i<this.videoCount;i++) {
        this.videoItem[i] = listObject.items[i];
    }

    this.getModifiedDateTime: function(epochtime) {
        var dateStringOutput = "";
        var myDate = new Date(epochtime);
        dateStringOutput = myDate.toLocaleString();
        return dateStringOutput;
    };
}

最佳答案

您应该将=运算符用于定义的方法(this.<methodName> = function (...) {)。

声明对象文字时使用冒号表示法。

关于syntax-error - Javascript类定义的麻烦定义方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2574785/

10-12 00:01
查看更多