Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Lets take the following example of inheritance in javascript:

var Employee = function(name, salary) {
  Person.call(this, name);
  this.name = name;
  this.salary = salary;
}
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.doWork = function() {
  alert('working!');
}

How about creating a Class and a super function so, that we could rewrite the previous code like this:

var Employee = Class({

  extends: Person

  constructor: function(name, salary) {
    super(name);
    this.salary = salary;
  }

  doWork: function() {
    alert('working!');
  }

});

The usage would be more or less analogous to ECMAScript 6 class:

class Employee extends Person {
  constructor(name, salary) {
    super(name);
    this.salary = salary;
  }

  doWork() {
    alert('working!');
  }
}

It still takes a some time until every browser runs ECMAScript 6, so why not use such shorthand for class declarations? Would this have any real drawbacks? Are there any libraries doing this?

share|improve this question
    
Well, TypeScript cross-compiles to EcmaScript 5. TypeScript has what you want and more. – Robert Harvey Sep 13 at 16:38
    
Note that the latest versions of Chrome, Firefox, Opera and Edge already support the vast majority of ES6. See kangax.github.io/compat-table/es6 – Robert Harvey Sep 13 at 17:11
    
Plus there are old libraries from before ES6 that already did what you describe – Izkata Sep 13 at 18:37
    
@Izkata Just out of curiosity, how are the called? – Gábor Angyal Sep 14 at 14:12
up vote 0 down vote accepted

It still takes a some time until every browser runs ECMAScript 6…

if you compile ES2015 code to ES5 with Babel, you can write it that way now anyway. No need for an awkward intermediary.

Would this have any real drawbacks?

A third syntax that attempts to mimic new features without actually being compatible with new features has the problem of proliferating unnecessary standards.

Are there any libraries doing this?

Not exactly; as I mentioned previously, Babel allows code to be compiled from one flavor of ECMAScript to another. I don't know of any intermediary frameworks, but I imagine it's likely that such things already exist in some form.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.