0

During trying to enhance Angular's ComponetFixture I noticed that this can not be done because of no copying constructor for this class. (Or am I wrong?)

Let's suppose we have a class:

class A
{
  constructor(public pub, private priv) { }
}

And I want to create class BetterA based on class A, so:

class BetterA extends A
{
  constructor(a: A)
  {
    // super(a);    <----- this can not be done, so maybe...

    // super(a.pub, a.priv)  // ...this could be better option, but...
  }

  myFunction(a: string) { return a; }
}

...second parameter is PRIVATE. I can not access it ;/

What can I do in that case?

I know that one of solutions is to use prototype like this:

A.prototype['myFunction'] = function(a: string) { return a; } // this must be done with function keyword, it's not working with ()=>{} !!! /there are problem with this pointer/

But then I have to write something weird like this:

console.log(  classAobject['myFunction']("abc")  );

Instead of

console.log(  classAobject.myFunction("abc")  );

or

I can do it by composition:

class B
{
  public a: A; // or constructor(public a: A)
  myFunction(a) { return a; }
}

But is seems not too elegant.

Is there any better solution?

Edit #1

I've just discovered that this syntax:

Class.prototype.NewFunction = function() { this.x.y.z = 123 }

is valid but it produces compiler errors, code works but we get:

'Property 'TextOf' does not exist on type 'Class'

and when you try to call it like this:

objectOfClass.NewFunction()

makes:

'Property 'NewFunction' does not exist on type 'Class'

BUT

It's gonna working only when we use function keyword. When we use lambda expression there will be same strange invisible problems with some functions.

tBlabs
  • 2,619
  • 3
  • 18
  • 22
  • Does your last example with composition solve your functional need? If so I see no problem with that. – Alex Feb 06 '17 at 22:19
  • This could solve my problem but now I need to change my code in many places instead of change the class declaration. I tried to use this http://stackoverflow.com/a/13897813/4584448 solution similar to the first one but without indexer notatnion ['myFunction'] but it's not working ;/ – tBlabs Feb 06 '17 at 23:10
  • I think many us us aren't familiar with how you need to use this. It would be awesome if you could provide sample code for the usage of `BetterA`. – Alex Feb 07 '17 at 13:56

1 Answers1

0

I think composition is the way to go here. please remember that you are building a class and not a method which requires the new operator in order to instantiate your object. this may be what your looking for

class A{
  tPub;
  constructor(public pub, private priv) {
   this.tPub=pub
  }
}
class B extends A{
 constructor(pub){
  super(pub)
 }
 myFunc(){} //equiv to B.prototype.myFunc
}

export const myClass=new B();


//another file
import {myClass} from './file'
let m=myClass.myFunc();

unfortunately, by setting priv to private it will do exactly what it is told and make it a private object. you also could do without the constructor depending on what you would like to do with your class.

Noah Wallace
  • 118
  • 7