问题描述
在多个地方(例如"创建MSDN上的C#和Visual Basic 中的用于JavaScript的Windows运行时组件"),我已经看到它指定,如果您要在.NET中编写要从JavaScript使用的类,则必须将其设置为密封类.
In several places (e.g. "Creating Windows Runtime Components for JavaScript, in C# and Visual Basic" on MSDN), I've seen it specified that, if you write a class in .NET that you want to use from JavaScript, then you must make it a sealed class.
这似乎是一个任意限制.为什么JavaScript只能与密封类一起使用?
This seems like an arbitrary restriction. Why can JavaScript only work with sealed classes?
推荐答案
从JavaScript的角度来看,暴露给JavaScript应用程序的Windows运行时对象是封闭的-您不能将expando属性添加到WinRT对象.但是从C ++和C#中,如果对象支持继承,则可以继承winrt对象(例如,大多数Xaml类支持继承,而大多数其他类则不支持).
Windows runtime objects exposed to JavaScript applications are sealed from a JavaScript perspective - you can't add expando properties to WinRT objects. But from C++ and C#, winrt objects can be inherited if the object supports inheritance (most Xaml classes support inheritance for instance, but most others don't).
将WinRT对象与JS隔离的原因是,无论应用程序执行了什么操作,都要确保Winrt对象的行为相同-如果应用程序在对象上重新定义了某些功能属性,则可能导致应用程序的其他部分行为不当.
The reason that WinRT objects are sealed from JS is to ensure that the winrt object behave the same regardless of what the app has done - if an app redefines some function property on an object, it could cause other parts of the app to misbehave.
这篇关于为什么必须密封WinRT类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!