Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
What's the quickest way to check if the browser supports private class feature?

I have a simple browser feature checking function called canUse() which can check for a variety of things, but checking for private class feature (i.e. #) support ended up like this:

canUse('(new (class { #private = 0; getPrivate() { return this.#private; } })).getPrivate();', 'eval')

Is there a faster way to check for this feature without using try...catch, eval(), and user agent sniffing?

JavaScript Code

'use strict';

function canUse(feature = null, type = 'member', attribute = '', value = '') {
  switch (type) {
    case 'member':
      return Boolean(feature);

    case 'callable':
      return typeof feature === 'function';

    case 'eval':
      return (() => {
        try {
          eval(feature);
          return true;

        } catch (error) {
          return false;
        }
      })();
  }

  const isElement = () => document.createElement(feature).constructor.name !== 'HTMLUnknownElement';
  const isAttribute = () => isElement() && attribute in document.createElement(feature);

  switch (type) {
    case 'element':
      return isElement();

    case 'attribute':
      return isAttribute();

    case 'value':
      return (() => {
        if (!isAttribute()) return false;

        const element = document.createElement(feature);
        element.setAttribute(attribute, value);
        if (element[attribute] !== value) return false;

        return true;
      })();

    default:
      return null;
  }
}

console.log('     Eval: ' + canUse('(new (class { #private = 0; getPrivate() { return this.#private; } })).getPrivate();', 'eval'));

console.log('   Member: ' + canUse(window.document, 'member'));
console.log(' Callable: ' + canUse(document.createElement, 'callable'));
console.log('  Element: ' + canUse('canvas', 'element'));
console.log('Attribute: ' + canUse('input', 'attribute', 'placeholder'));
console.log('    Value: ' + canUse('input', 'value', 'type', 'file'));

Console Output Console output of the JavaScript code

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!