• Авторизация


Spidermonkey Development Blog: Ergonomic Brand Checks will ship with Firefox 90 18-05-2021 17:44 к комментариям - к полной версии - понравилось!


When programming with Private Fields and methods, it can sometimes be desirable to check if an object has a given private field. While the semantics of private fields allow doing that check by using try...catch, the Ergonomic Brand checks proposal provides a simpler syntax, allowing one to simply write #field in o.

As an example, the following class uses ergonomic brand checks to provide a more helpful custom error.

class Scalar {
  #length = 0;

  add(s) {
    if (!(#length in s)) {
      throw new TypeError("Expected an instance of Scalar");
    }

    this.#length += s.#length;
  }
}

While the same effect could be accomplished with try...catch, it’s much uglier, and also doesn’t work reliably in the presence of private getters which may possibly throw for different reasons.

This JavaScript language feature proposal is at Stage 3 of the TC39 process, and will ship in Firefox 90.

https://spidermonkey.dev/blog/2021/05/18/ergonomic-brand-checks.html

вверх^ к полной версии понравилось! в evernote


Вы сейчас не можете прокомментировать это сообщение.

Дневник Spidermonkey Development Blog: Ergonomic Brand Checks will ship with Firefox 90 | rss_planet_mozilla - Planet Mozilla | Лента друзей rss_planet_mozilla / Полная версия Добавить в друзья Страницы: раньше»