2

TL;DR:

  1. I want to pass object as a parameter when creating a new instance of a class (ChildClass)

  2. When I'm passing the parameters I want to save the context so .this should refer to the object where this instance was creating (not the context of the reference)

parent.class.js

class ParentClass {
    constructor(nativeObject, param1, param2) {
        this.nativeObject = nativeObject;
        this.param1 = param1;
        this.param2 = param2;
    }
    // some logic here
}
module.exports = { ParentClass };

child.class.js

class ChildClass extends ParentClass {
    // some logic here
}
module.exports = { ChildClass };

usingclasses.js

let { ChildClass } = require('path/to/my/child/class');

let param1 = 'idkSomeString';
let param2 = 42;

class MyCoolClass {
    createChild() {
        return new ChildClass(this, param1, param2);
    }
}
module.exports = { MyCoolClass };

As you can see I'm passing here this bc in ParenClass I need to know (that logic is not included in the example) where that instance of the ChildClass was created. There are many files like usingclasses.js with different classes but each has it's own createChild() method which creates the ChildClass.

What I want to achieve

usingclasses.js

let { ChildClass } = require('path/to/my/child/class');

let paramsObj1 = {
    nativeObject: ' ', // ?????,
    param1: 'idkSomeString',
    param2: 42
};

let paramsObj2 = {
    nativeObject: ' ', // ?????,
    param1: 'idkSomeString',
    param2: 42
};
class MyCoolClass {
    createChild() {
        return new ChildClass(paramsObj1 );
    }

    createAnotherChild() {
        return new ChildClass(paramsObj2);
    }
}

So first question is: How can I store all the params in a separate variable (object) at the top so I can simply do

new ChildClass(objectWithParams1) ,

new ChildClass(objectWithParams2)

...

new ChildClass(objectWithParamsN)

And how can I pass the context (first argument in ParentClass constructor) so it will refer to the object where the instance of this class was created?

I do realize that if I will do

let params = {
    nativeObject: this,
    param1: 'idkSomeString',
    param2: 42
};

this will refer to the params object but I need the constructor to know that it was created in the MyCoolClass

UPDATE After researching I'm curious does .bind has something do to with this kind of issues. When I actually need to specify what this is referring to?

Can I somehow specify that this is referring to MyCoolClass?

This will allow me to store everything at the top and then just pass the one single object as parameter when creating new instance of the ChildClass

anotheruser
  • 582
  • 1
  • 7
  • 23
  • 3
    *"I do realize that if I will do ... `this` will refer to the `params` object"* No, it won't. It'll refer to whatever `this` is where that object literal is. – T.J. Crowder Dec 13 '19 at 10:27
  • @T.J.Crowder you are right. Maybe I should wrote "I tried it and that did not set the nativeObject to MyCoolClass". I guess it's because the prams variable is declared before MyCoolClass? – anotheruser Dec 13 '19 at 10:33

1 Answers1

0

The simple way to do it is the way you showed in your first example (but keep reading for an alternative): By passing this into ChildClass as an argument:

class MyCoolClass {
    createChild() {
        return new ChildClass(this, paramsObj1);
    }

    createAnotherChild() {
        return new ChildClass(this, paramsObj2);
    }
}

ChildClass's constructor would look like:

constructor(parentObject, params) {
    super(parentObject, params.param1, params.param2);
}

or

constructor(parentObject, {param1, param2}) {
    super(parentObject, param1, param2);
}

But if you really want to pass just one thing into ChildClass, you'll have to assign it from within MyCoolClass's code, perhaps like this:

class MyCoolClass {
    createChild() {
        return new ChildClass({parentObject: this, ...paramsObj1});
    }

    createAnotherChild() {
        return new ChildClass({parentObject: this, ...paramsObj2});
    }
}

ChildClass's constructor would look like:

constructor(params) {
    super(params.parentObject, params.param1, params.param2);
}

or

constructor({parentObject, param1, param2}) {
    super(parentObject, param1, param2);
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • That's a great idea to start with. I just need one clarification: Do I really have to include constructor in ChildClass (because it actually does not have one it just inherits the parent one)? Can I just change the very first constructor (in ParentClass) to retrieve object the way you showed? – anotheruser Dec 13 '19 at 10:39
  • 1
    @anotheruser - Yes, that's absolutely fine. If you don't have a constructor in `ChildClass`, the JavaScript engine will create one for you that looks like this: `constructor(...args) { super(...args); }`. So if `ParentClass` expects what `MyCoolClass` passes to `ChildClass`, you're good. :-) – T.J. Crowder Dec 13 '19 at 10:48
  • I'm marking you answer as correct. It helped me a lot. If you are looking forward helping newbies please check my next question: https://stackoverflow.com/questions/59387568/a-way-to-retrieve-the-context-without-passing-this-as-parameter – anotheruser Dec 18 '19 at 07:49