Closed warappa closed 11 months ago
Thanks @warappa for reporting this issue. My point of view:
public static class TopazObjectExtensions
{
#pragma warning disable IDE1006 // Naming Styles
public static bool hasOwnProperty(this object obj, string name)
{
if (obj == null) return false;
if (obj is IDictionary dic)
return dic.Contains(name);
var prop = obj.GetType().GetProperty(name);
return prop != null;
}
#pragma warning restore IDE1006 // Naming Styles
}
void TestExtensionMethod() { var engine = new TopazEngine(); engine.AddExtensionMethods(typeof(TopazObjectExtensions)); var someObject = new TestDto { SomeProperty = 123 }; engine.SetValue("someObject", someObject); var result = engine.ExecuteExpression("someObject.hasOwnProperty('SomeProperty')"); Console.WriteLine(result); }
Sorry, I see I should have picked feature request.
Thank you for the example of the alternative way, I will try it.
Topaz does not enforce full Javascript API compatibility
This is an important point for me to consider as I would have to mimic the JavaScript API as far as we use it.
Topaz does as few as possible to keep the engine simple and fast and provides extension points. Please consider that not all consumers would need fully capable Javascript API.
For all extension opportunities, please refer to TopazEngineSetup
The dotnet CLR objects exposed in Topaz Runtime are not JavaScript objects and by default they don't implement standard JavaScript object methods.
The JsObject is designed to mimic Javascript Objects and implements a few essential JavaScript object methods.
Users are responsible for providing methods callable by the JavaScript runtime for the exposed CLR objects.
I am closing this feature request because there is a viable method to achieve the desired functionality.
Describe the bug The example I had shown you in #18 works, but
hasOwnProperty
is still missing functionality. In JavaScript,hasOwnProperty
works on any object, no matter what it is.Expected behavior
hasOwnProperty
should work for any object, regardless if it implementsConcurrentJsObject.cs
orIDictionary
or something else. To do this it may even utilize reflection if it has to.Reproduction Given a class like
running the following code should return
true
Desktop