"Unexpected token this" from JS class?

时间:2019-04-16 23:03:10

标签: javascript es6-class

I can't figure out why I'm getting this error: unexpected token this. I have a class that's going to store a lot of properties. One of the properties, rawData, will be a multi-dimensional array that comes from a CSV. One of the methods will check the 1st array (i.e. zeroth) to see if it's numbers or labels. I will not be able to pass the CSV data at the time of object creation, it must be set later. I've simplified my code here to make it easier to read.

//DropProperties Class
class Droperties{
    rawData;

    constructor(){

    }

    isFirstRowLabel(this.rawData){
        if(this.rawData[0].some(isNaN)){
            return true;
        } else {
            return false;
        }
    }
}


var droperties = new Droperties();
droperties.rawData = [
    ['Orks', 'Imperial Gaurd', 'Space Marines', 'Chaos Daemons', 'Tyranids', 'Elda'],
    [5, 2, 3, 4, 5, 6]
];
console.log(droperties.isFirstRowLabel);

Can someone shed some light on how to approach this issue?

1 个答案:

答案 0 :(得分:0)

正如JJJ所说,我不需要将类属性作为参数传递。另外,我似乎丢失了console.log(droperties.isFirstRowLabel);

上的空括号

以下作品:

//DropProperties Class
class Droperties{
    rawData;

    constructor(){

    }

    isFirstRowLabel(){
        if(!this.rawData[0].some(isNaN)){
            return true;
        } else {
            return false;
        }
    }
}


var droperties = new Droperties();
droperties.rawData = [
    ['Orks', 'Imperial Gaurd', 'Space Marines', 'Chaos Daemons', 'Tyranids', 'Elda'],
    [5, 2, 3, 4, 5, 6]
];
console.log(droperties.isFirstRowLabel());