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
I am exposing a few types inheriting from CustomType, for example:
public class SampleObject : CustomType
{
public Sample Sample => (Sample)Value;
public SampleObject(Sample sample)
{
Value = sample;
}
protected override JSValue GetProperty(JSValue key, bool forWrite, PropertyScope propertyScope)
{
if (key.Value is string propertyName)
{
switch (propertyName)
{
case "registerId": return Sample.Register.Id;
...
case "unitId": return Sample.Unit.Id;
}
}
return Undefined;
}
Now consider the following example:
getSample().registerId
This correctly invokes the GetProperty method on SampleObject as this is the returned type from getSample(). However, if I assign the result from getSample() to an intermediate variable, then the value get cloned, and the new instance becomes of type JSValue because a new instance in created:
So, when accessing properties on this cloned instance (for example after variable assignment), then accessing the property throws:
const sample = getSample();
sample.registerId; // this throws with "Method GetProperty(...) of custom types must be overridden"
CloneImpl() is internal and so it is not possible to override it so that the returned instance on cloning becomes of the same type as the original returned instance.
How does one inherit from CustomType and make sure that this type propagate on object cloning?
The text was updated successfully, but these errors were encountered:
Do not override property Value and content of this property. When you assign result of getSample() to variable, you get a raw instance of Sample without proxy, not SampleObject. Value must contains this. It is how works mechanism of variables.
Also, for cases, when property is unknown is more correct to return NotExistsInObject. This is matter for operators like in and delete.
Yet another "also": CustomType is not required for custom types. You can inherit JSValue directly. Look at this example. CustomType is kind of sugar.
I am exposing a few types inheriting from CustomType, for example:
Now consider the following example:
This correctly invokes the GetProperty method on SampleObject as this is the returned type from getSample(). However, if I assign the result from getSample() to an intermediate variable, then the value get cloned, and the new instance becomes of type JSValue because a new instance in created:
NiL.JS/NiL.JS/Core/JSValue.cs
Lines 800 to 812 in 0c68c77
So, when accessing properties on this cloned instance (for example after variable assignment), then accessing the property throws:
CloneImpl() is internal and so it is not possible to override it so that the returned instance on cloning becomes of the same type as the original returned instance.
How does one inherit from CustomType and make sure that this type propagate on object cloning?
The text was updated successfully, but these errors were encountered: