0

I apologize in advance if this question is poorly worded. How could I do something like this:

class MyClass {
  seeName() {
    // ...
  }
}

const foo = new MyClass();
console.log(foo.seeName()); // prints "foo"

const bar = new MyClass();
console.log(bar.seeName()); // prints "bar"
TacoSnack
  • 129
  • 10

1 Answers1

0

You could do this:

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

  seeName() {
    return this.name;
  }
}

const foo = new MyClass("foo");
console.log(foo.seeName()); // prints "foo"

const bar = new MyClass("bar");
console.log(bar.seeName()); // prints "bar"

Then the name you pass in when you create the object will be stored in the object, but it won't actually know the name of the variable it's stored in.

If you want an better anwser specify what you want to do with the name and what your trying to do in general.

Anton
  • 563
  • 4
  • 13