Sunday, May 17, 2015

Chrome and Shadow Constructors

Up until now we've discussed that a constructor object is a highly visible (attached to the Global by name) function that provides you access to the type system of a given type. While the prototype describes what you can do with a type and in fact is stored on each instance, the constructor is how you begin playing with a type system before you have any knowledge of what might be available.

That is why the next type of constructors, for which we currently have no WebIDL syntax, is actually kind of interesting. I'm calling them Shadow Constructors, though my first thoughts on a proper naming scheme are to use a custom attribute [Hidden] on the interface definition to note that when building the constructor itself, it should not be attached to the Global namespace. Were we to implement such a feature only in EdgeHTML and before WebIDL had such support we'd probably define it as an [msPrototypeObjectOnly]. This would be in stark contrast to the previously described [msInterfaceObjectOnly] which describes a constructor from which no instances can be made.

So what does this odd entity look like and why might it be important?

StyleMedia

The StyleMedia constructor is pretty much dead. FireFox has acknowledge this and removed it completely. There is no StyleMedia constructor and there is no window.styleMedia property instance to retrieve.

Chrome has taken a different approach. They removed the StyleMedia constructor, but they still have a window.styleMedia property and in fact, this also returns an instance, whose constructor is, you guessed it, StyleMedia. So how is that even possible? Well, they clearly implemented some form of Shadow constructors to make this happen. While I can't comment on how this works in their system, in my own FastDOM implementation it would simply be a flag controlling whether or not I set the constructor into its slot on the Global after creation.

Finally, in EdgeHTML, we still have this legacy. Primarily, since we support the instance and we tend towards a purist view that if you can have an instance you should be able to both a) find and b) impact its type system using standard methods. That said, the differences in all 3 browser type systems is concerning and maybe worth a look at doing something. My first proposal would be to remove the legacy styleMedia property. That cleans up the most, but if this is untenable as determined via use counters then Shadow constructors are a good middle-ground I suppose.

What do you guys think about Shadow constructors? Do they seem useful as a tool for slow deprecation? Could they be used to hide Browser specific capabilities by NOT exposing them on the Global?

Feel free to have a conversation in the comments or reach me @JustRogDigiTec.

No comments:

Post a Comment