2

I try to access the properties of my "Foo" class. My code looks like this:

class Foo {
  constructor(myname) {
    this.myname = myname;
  }
}

Foo.prototype.get = {
  name() {
    return this.myname;
  }
};

const TestClass = new Foo('Steven');
TestClass.get.name(); // undefined

Its clear that in TestClass.get.name the myname property does not exists because the this context is TestClass.get

No I am looking for a recommended way to access myname. Smth like: Parent.myname.

TJR
  • 6,307
  • 10
  • 38
  • 64
  • That is because, your `this` is pointing to `get` and not instance. Also its a bad idea having objects in prototype – Rajesh Aug 21 '17 at 12:51
  • If you really want the `get` to be an object, you can do `this.get = { ... }` in the constructor. There is no native way to access the variables in *parent* contexts. – Ionică Bizău Aug 21 '17 at 12:56
  • If you really want to access using a get property, You can't add the get to the prototype, as the prototype is for every instance. But you could add the get to the object, and then this get could also be a class.. – Keith Aug 21 '17 at 13:00

2 Answers2

1

In your example context of calling .name() is object: TestClass.get, which is { name: function() {...} }. But you need to call this method with TestClass as a context:

 TestClass.get.name.call(TestClass)
Ivan Minakov
  • 1,432
  • 7
  • 12
1

If you are using ES6 classes, I recommend writing your accessors like this :

class yourClass {
    constructor(myname) {
        this.myname = myname;
    }

    get name() {
        return this.myname;
    }
}

Example code using that class :

var foo = new yourClass("Harold")
console.log(foo.name)

Note that you can access them like an field, even if you are basically running a function.

Seblor
  • 6,947
  • 1
  • 25
  • 46
  • Thanks for your idea. My example show's a simple get but in most of my cases the structure is not like this. Its more like a SDK for an API. Example: SDK.SDK_TOPIC.SDK_ACTION(); But the call of SDK_ACTION needs to access properties from the created object of class. – TJR Aug 21 '17 at 12:56
  • I don't see the issue, actually. I think I'd need to see your code to understand, honestly. – Seblor Aug 21 '17 at 12:58
  • I think he want to access using a property called `get` for a double dot notation.. eg. for a deeper example.. `shape.color.background = 123`, or similar. It is possible to create these sort of class structures but it's not trivial. – Keith Aug 21 '17 at 13:02
  • The linked question on stackoverflow did the thing I want to do. When you look for example how the paypal sdk is structured you see what I want to do. PayPal works for example like this: PayPal.payment.create - I dont know if PayPal needs the this context in create function but in my case I need it. But however: The linked stackoverflow question did it very well :) – TJR Aug 21 '17 at 13:06
  • I guess `PayPal` is an object with a `payment` field, which has a `create` method. I did not work with it, though. – Seblor Aug 21 '17 at 13:07