BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Deno 1.40 Released, Features Upcoming JavaScript’s Temporal API and Decorators

Deno 1.40 Released, Features Upcoming JavaScript’s Temporal API and Decorators

This item in japanese

The Deno team recently released Deno 1.40. Deno 1.40 implements upcoming JavaScript’s Temporal API for advanced date and time operations; and the latest JavaScript’s decorator proposal for meta- and aspect-oriented programming.

The Temporal API addresses shortcomings in JavaScript’s Date object. As Maggie Johnson-Pint explained a few years ago:

We identified the basic problems with the current Date implementation.

Here they are, in order of relative disgust:

  1. No support for time zones other than the user’s local time and UTC
  2. Parser behavior so unreliable it is unusable
  3. Date object is mutable
  4. DST behavior is unpredictable
  5. Computation APIs are unwieldy
  6. No support for non-Gregorian calendars

For more details on the API, developers can review the Temporal cookbook. The Temporal API proposal is Stage 3 and will officially become part of the JavaScript language when it moves to Stage 4,

The JavaScript’s decorator proposal is also Stage 3 and enables developers to extend JavaScript classes.

Decorators are functions called on classes, class elements, or other JavaScript syntax forms during definition. Here is an example of an @trace decorator that logs out whenever a function is called, and when it returns:

function trace(fn: any, ctx: ClassMethodDecoratorContext) {
  return function (...args: unknown[]) {
    console.log("ENTERED", ctx.name);
    const v = fn(...args);
    console.log("EXITED", ctx.name);
    return v;
  };
}

class App {
  @trace
  static start() {
    console.log("Hello World!");
  }
}

App.start();

As the proposal explains:

One of the main reasons decorators are still being pursued today, and specifically the main reason class decorators are an important language feature, is that they fill a gap that exists in the ability to metaprogram in JavaScript.

Decorators have been made popular by Angular 2 (now simply Angular), the current proposal however is the fourth iteration of the proposal and is expected to be the final version that will ultimately get added to the specification when the proposal process is complete. The TC39 (committee standardizing the JavaScript language) has been iterating on the decorators’ proposals for over five years.

Developers are encouraged to review the original release note, which includes the full list of features, improvements, and bug fixes. Deno is open-source software available under the MIT license. Contributions are encouraged via the Deno Project and should follow the Deno contribution guidelines.

About the Author

Rate this Article

Adoption
Style

BT