Answer the question
In order to leave comments, you need to log in
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
(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
}());
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question