As I was attempting to improve Angular's ComponetFixture, I discovered a limitation due to the absence of a copying constructor for this class. (Or am I mistaken?)
Imagine we have a class:
class A
{
constructor(public pub, private priv) { }
}
Now, if I want to create a class BetterA based on class A:
class BetterA extends A
{
constructor(a: A)
{
// super(a); <----- this is not possible, so maybe...
// super(a.pub, a.priv) // ...this could be a better option, but...
}
myFunction(a: string) { return a; }
}
Unfortunately, the second parameter is PRIVATE. Thus, I cannot access it.
What can be done in this situation?
I am aware that one solution 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/
However, it requires writing something like:
console.log( classAobject['myFunction']("abc") );
Instead of:
console.log( classAobject.myFunction("abc") );
or
Another approach could be through composition:
class B
{
public a: A; // or constructor(public a: A)
myFunction(a) { return a; }
}
Yet, it may not seem very elegant.
Is there a better solution available?
Edit #1
I've recently found out that this syntax:
Class.prototype.NewFunction = function() { this.x.y.z = 123 }
is valid, but it results in compiler errors. The code works, but we encounter:
'Property 'TextOf' does not exist on type 'Class'
and when trying to call it like this:
objectOfClass.NewFunction()
it shows:
'Property 'NewFunction' does not exist on type 'Class'
However,
It only works when using the function keyword. When using a lambda expression, strange invisible problems with some functions might occur.