v151 · web platform · fonts

instanceof Guards

Exposing the FontFaceSet interface object is what makes value instanceof FontFaceSet legal. Type-guard functions in libraries and frameworks rely on that — with [LegacyNoInterfaceObject] the same guard threw a TypeError because the right-hand side was not callable. Pick a value and run a real guard against it.

Pick a value and press “Run guard”.

guard across all values

valueisFontFaceSet(value)

the guard

// Only works once the interface object is exposed on the global
function isFontFaceSet(value) {
  return typeof FontFaceSet === "function" && value instanceof FontFaceSet;
}

// A plugin that accepts either a document's font set or a standalone one:
function preload(fontSetOrDoc) {
  const set = isFontFaceSet(fontSetOrDoc) ? fontSetOrDoc : fontSetOrDoc.fonts;
  return set.ready;
}

Before Chrome 151, the bare value instanceof FontFaceSet threw "Right-hand side of 'instanceof' is not callable" in Chrome, so cross-browser code had to fall back to duck-typing (checking for .check and .ready). Now the direct, precise instanceof check works everywhere.

see also

references