IT Mrav Logo

IT Mrav

The Way I Wished JavaScript Went - A New Year's Wish 🌲

As we approach the new year, let's dive in a programming fantasy and evolution of JavaScript that seamlessly blends its prototype-based nature with a cleaner, more expressive syntax influenced by Rust. Picture a world where defining objects maintains the familiar prototype structure but is enriched with a syntax that brings joy to developers.

ES5-style Prototype Definition:

function Person(name) {
    this.name = name;
}

Person.prototype.print = function() {
    console.log(this.name);
}

In the well-known ES5-style prototype definition, we create a Person function and enhance its prototype with a print method. While this has been the JavaScript convention for a while, imagine infusing it with a syntax that's both cleaner and more expressive.

On JavaScript's Evolution

I think that JavaScript, post-ES5, went off course with its current definition. Unfortunately, I can't turn back time, and the inertia from other languages, combined with the current state of JavaScript, is too great. People tend to favor what is familiar, a landscape is filled with millions of packages and hundreds of super-set languages attempting to address the complexities that are created by a "bad patch". But that can't stop us wishing!

The New Year's Wish:

// Prototype Definition
prototype Person {
    name: string;
}

// Trait Declaration
trait Printable {
    print();
}

// Trait Implementation for Person
trait Printable for Person {
    print() {
        console.log(name);
    }
}

// Type-checked,converted and sanitized
let myPerson: Person = { name: "John Doe" }; 
print(myPerson);
// this allow for recommendation
// of all objects in scope that support print
// To avoid ambiguity, opt.:
print<Printable>(myPerson);
// This should be allowed
myPerson.print(); 

In this wishful vision, the Person prototype is defined in a concise and readable manner using explicit syntax, eliminating constructor boilerplate. To maintain JavaScript's extensibility and composability, traits are introduced.

Traits in JavaScript?

Yes, imagine traits seamlessly integrated into JavaScript. In this wish, we use the trait keyword to declare a Printable trait with a print method. We then apply this trait to the Person prototype, allowing it to inherit and implement the behavior.

Type Checking

The usage example demonstrates not only a cleaner syntax but also introduces type checking. The variable myPerson is explicitly declared as a Person type, ensuring type safety during compilation/interpretation (who knows!). Additionally, the print function showcases how type ambiguity can be resolved, offering a more robust and predictable development experience.

This syntax harmoniously combines the best of both worlds, returning the prototype-based structure familiar to older JavaScript developers while introducing a clean and expressive syntax. The result? A wishful JavaScript that's not only powerful but also a joy to write and read.

Who knows what the future holds for JavaScript? Perhaps wishes like these might find their way into the language, making the developer experience even more delightful. 🍀

I know this proposition can be unfavorable to many, so bring your toughs to the table and lets discuss. 🔥

Until then, let's keep dreaming and coding! 🚀✨

link_round [#1110]Created with Sketch.

More blogs

new notification

Thanks for reading! ^_^

Contact

Slobodan at It Mrav reply email [#1573]Created with Sketch.It Mrav link_round [#1110]Created with Sketch. facebook [#176]Created with Sketch.
Slobodan Živković

the Owner