Class method returns null

Asked

Viewed 37 times

-1

I have a class to pass data but when I call the class method this data appears as null. Why?

class Test {

    constructor(data) {
        this.myData = data;
    }

    MyFunction = () => {
        return this.myData;
    };
}

When I install this Class the console returns to me null

<script type="text/javascript">

    var t = new Test({"data1", "data2", "data3"});
    //
    console.log( t.MyFunction() );

</script>
  • 1

    You probably want to MyFunction (){ instead of MyFunction = () => {

1 answer

2


Class methods shall be declared with : () => { or abbreviating directly to (){... or as we would do in an object.

in addition, the syntax of that data that you pass to class when instances are wrong. You are using {'string', 'string'}... I think you want to [] to get an array.

Example:

class Test {

  constructor(data) {
    this.myData = data;
  }

  MyFunction() {
    return this.myData;
  };
}

var t = new Test([
  "data1",
  "data2",
  "data3"
]);

console.log(t.MyFunction());

  • The problem was one of my functions was static and should not, but thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.