scriptsharp icon indicating copy to clipboard operation
scriptsharp copied to clipboard

Inline (outside constructor) field initialization code is not always added to constructor.

Open vgribok opened this issue 14 years ago • 2 comments

Class member field initialization should generate same JS code regardless where initialization is done: inside constructor or outside. Currently initialization seems to be converted reliably only if done inside the constructor.

public abstract class Whatever { private int nField = -1; // gets added to constructor generated JS code private bool bField = true; // does NOT get added to the JS constructor.

public Whatever()
{
    this.bField = true; // gets added to JS constructor.
}
...

vgribok avatar Jul 25 '11 19:07 vgribok

Good catch. Agree this should be fixed. All the member fields will eventually be moved into the ctor rather than being placed on the prototype.

If you're curious why this happens, right now, only members initialized to literals are placed on the prototype. bField is being set to true, which is a literal. nField is being set to -1, which is a unary expression, even though it appears like a literal to the developer. For example if nField was initialized to 1, it would go on the prototype.

nikhilk avatar Aug 07 '11 02:08 nikhilk

Interesting, thanks for the explanation.

robitar avatar Aug 15 '11 09:08 robitar