You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I define a class that has a field with default values, in Java it's the same if the value is set directly to the field or if it's set in the constructor. The generated javascript is different with different behaviour:
public class Data
{
public String str1="String 1";
public String str2;
public Data() {
str2="String 2";
}
}
In javascript "str1" is set in the prototype, "str2" is set in the constructor. The "new Data().str1" and "new Data().str2" both give the same result but JSON.stringify(new Data()) only contains str2.
The text was updated successfully, but these errors were encountered:
This is indeed caused by the fact that we put the default values of instance fields into the prototype of the class. One of the side effect of doing this is that hasOwnProperty() for those fields will return false if the value has not been overwritten.
JSON.stringify() only serializes the own properties of objects, and skips anything that exists only in the prototype.
As a workaround, assign your fields in the constructor until the problem is fixed.
Moving this to version 4. Fixing this requires a pretty big refactoring of the way class inheritance is done, and of the way instance fields are initialized. On the positive side, if we fix this, we should also be able to remove all the limitations we have currently placed on initializing fields out of the constructor (currently only constants are allowed, IIRC)
When I define a class that has a field with default values, in Java it's the same if the value is set directly to the field or if it's set in the constructor. The generated javascript is different with different behaviour:
public class Data
{
public String str1="String 1";
public String str2;
public Data() {
str2="String 2";
}
}
In javascript "str1" is set in the prototype, "str2" is set in the constructor. The "new Data().str1" and "new Data().str2" both give the same result but JSON.stringify(new Data()) only contains str2.
The text was updated successfully, but these errors were encountered: