0

I have created two classes:

class MyFirstClass {
    constructor(fname, lname, date) {
        this.fname = fname;
        this.lname = lname;
        this.date = date;
    }
}

class MySecondClass extends MyFirstClass {
    constructor(id, optionalArg) {
        super();
        this.id = id;
    }
}

And then created two objects like this:

let myFirstObj = new MyFirstClass("foo", "bar", "today");
let mySecondObj = new MySecondClass(1234); //***(1234, optionalArg)

Now, there are several ways to actualy pass properties from first class to another (or from first obj to second), but whatever I do second object doesnt REFER to the first one, it just creates its own "copy" of properties. So when I do this:

mySecondObj.fname = "someothername";

first object doesnt change - its not referenced to the second one (or the other way - doesnt work either). My question is: How to solve this "conection" on classes (or out of them) to actualy reference new objects one to another? I want to make it as simple as possible (thats why I left optional argument in second class).

Bambino Negro
  • 115
  • 1
  • 8
  • What overall problem are you trying to solve? This appears to be rather unusual. – Felix Kling Feb 22 '19 at 00:19
  • To somehow expand second class props (or object props) by easy sintax to make new objects from two classes that one extends on another - actualy refer one to another. I've tried few ways, but each one failed to create two objects that are refered one to another – Bambino Negro Feb 22 '19 at 00:25

2 Answers2

1

Here is a try using get and set to maintain the reference connection between two object.

See below and read the comment.

class MyFirstClass {
    constructor(fname, lname, date) {
        this.f_name = fname;
        this.f_name = lname;
        this.d = date;
        this.b = this;
       
    }
    
    set base(value){
        this.b = value;
        var a = this;
        
        Object.keys(this).forEach(function(key){
           if (a.b[key] != undefined)
               a[key] =a.b[key]
        });
    }
    
    set fname(value){
      this.f_name = this.b.f_name = value; 
    }
    
    get fname(){
      return this.f_name;
    }
    
    get lname(){
     return this.l_name; 
    }
    
    set lname(value){
      this.l_name =this.b.l_name= value; 
    }
    
    set date(value){
      this.d =this.b.d= value; 
    }
    
    get date(){
       return this.d;
    }
    
}

class MySecondClass extends MyFirstClass {
    constructor(id,base,optionalArg) {
        super();
        this.id = id;
         // chooce to have a reference to another object as base
         // now when you change either this or base class the both of them will change to
        if (base){
            this.base = base;
            base.b = this;
           }
    }
}

let myFirstObj = new MyFirstClass("foo", "bar", "today");
// myFirstObj is optional. 
let mySecondObj = new MySecondClass(1234, myFirstObj); //***(1234,base, optionalArg)

mySecondObj.fname = "test"

console.log(myFirstObj.fname)
Alen.Toma
  • 4,684
  • 2
  • 14
  • 31
  • Sorry, did it right now. There have been left one issue thau (its not "fully" referable - other way around - if you change first obj args, second ones doesnt change). Anyway for now this solution works for me. – Bambino Negro Feb 22 '19 at 01:21
  • Now it should work as you wanted, if you hange firstobject the secoundobject will change and when you change the secound the first will change to. – Alen.Toma Feb 22 '19 at 01:31
  • I was just hoping that I am missing somewhere "bind" or "apply" or "call" :) , and I was persistant to somehow call some of these methods somewhere on creation (on "new" - because you already have named but undefined props on calling super() - and wanted somehow to bind all these props to props of previous created Obj), but as you probably already knew - you can not apply this on creating. :( – Bambino Negro Feb 22 '19 at 01:36
  • Yes you could create the set and get dynamicly, read this. https://stackoverflow.com/questions/812961/javascript-getters-and-setters-for-dummies – Alen.Toma Feb 22 '19 at 01:40
  • Np glad i could help – Alen.Toma Feb 22 '19 at 01:40
0

Creating class inheritance does not mean they are implicitly sharing values by reference. So myfirstobj and mysecondobj would occupy their own space in memory, and changes made to either of them won't automatically change their counter part.

  • Of course, but I need new created objects to be referenced one to another. How to acomplish this? – Bambino Negro Feb 22 '19 at 00:11
  • @BambinoNegro make it explicit: if you want an object to have a reference to another object - pass it as an argument. – zerkms Feb 22 '19 at 00:12
  • zerkms - I need them both to be created this way - by two classes that are extended one on another, I dont mind if some "intervention" is applied latter to acomplish that (or inside classes). Just need this kind of "tidiness". – Bambino Negro Feb 22 '19 at 00:14
  • 1
    @BambinoNegro then you need to elaborate your original problem, what you request makes very little sense – zerkms Feb 22 '19 at 00:16
  • What you could do is maybe pass the objects .this as an arguement, and bind the .this in the class constructor to the .this in the parameter class person (){ constructor(newThis){ this = newThis; } } –  Feb 22 '19 at 00:17
  • @sosa067: You cannot assign to `this`. – Felix Kling Feb 22 '19 at 00:18
  • @FelixKling but you can assign this on a function level no? –  Feb 22 '19 at 00:18
  • You can set the `this` value of a "normal" function when calling it via `.call` or `.apply`, but `this` cannot appear on the left hand side of an assignment expression. – Felix Kling Feb 22 '19 at 00:20
  • Then I guess another way is to pass the actual object into the constructor –  Feb 22 '19 at 00:21
  • when you do that, you get two objects that are not referenced one to another, and that is the problem. – Bambino Negro Feb 22 '19 at 00:22
  • zerkms - I can not elaborate my problem because this is the task. :D – Bambino Negro Feb 22 '19 at 00:23
  • @BambinoNegro just to be clear, what you are doing here is not passing the objects to each other, you are just creating inheritance have you tried actually passing the object to the constructor? like let secondobj = secondobj(firstobj) –  Feb 22 '19 at 00:23
  • Y, thats why I left "optionalArg" unused - I've tried several ways but all ended by creating two not referenced objects. - Each time it failed to reference one to another. I am searching for the way to achieve that. – Bambino Negro Feb 22 '19 at 00:30