1

I want to be able to put a method of a class in a separate file but I can't work out how to do it.

This is what I am thinking at the moment, but it doesn't work:

index.js:

import Person from "./Person.js";

let billy = new Person("Billy");
billy.sayName();

Person.js:

import sayName from "./sayName.js";

class Person{
  constructor(name){
    this.name = name;
  }
}

Person.prototype.sayName = sayName;

export default Person;

sayName.js:

//I get an error: 'thread 'main' has overflowed its stack'
export default function sayName () {
  console.log(this.name);
}

0 Answers0