K
K
Kirill Khalitov2015-03-24 16:36:50
JavaScript
Kirill Khalitov, 2015-03-24 16:36:50

Why are classes in ES6 so miserable?

Hello.
Interested in the new ES6 standard. Looked at implementation of classes and was disappointed. What I didn’t like the most was that in the new standard you can’t declare fields in the body of a class declaration. Even in ES5, we could mimic private fields in a constructor function using a closure.

var Person = (function()
{
  var name;

  function Person(_name)
  {
    name = _name;
  }

  Person.prototype.getName = function()
  {
      return name;
  };

  return Person;
}());

var p = new Person("Kirill");
console.log(p.getName()); // Kirill

I looked at the old coder tricks using Symbol and WeakMap to simulate privacy in ES6 and was still dissatisfied.
(function()
{
  const name = Symbol();
  
  class Person
  {
    constructor(_name)
    {
      this[name] = _name;
    }
    getName()
    {
      return this[name];
    }
  }
  
  var p = new Person("Kirill");
  console.log(p.getName()); // Kirill
}());

Why are ES6 classes so unfinished? Would have done at least like in TypeScript. Although there is private and not private, at least there are normal fields.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KorsaR-ZN, 2015-03-24
@Voronar

Yes, because it's just syntactic sugar and nothing more.
Just as there were no classes in JavaScript, there are none...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question