在 javascript 中使用 类 而不是构造函数会影响性能吗?

Does using classes in javascript instead of constructors give a performance hit?

我假设因为 javascript 中的 class 只是语法糖,所以 class 在某些时候被转换为构造函数,所以我也假设他的为引擎创建额外的事情,这是一个准确的假设吗?

 //Class, is this slower then a constructor   
class Employee{
    constructor(name,job,weeklyPay,age){
        this.name=name;
        this.breed=breed;
        this.age=age;
    }
    pay(){
        console.log(this.name + ": was paid " + weeklyPay);
    }
}

class Manager extends Employee{
     constructor(){
        this.reports=[];
     }
    addReport(report){
        this.report.push(report);
    }

    removeReport(index){
        this.report.splice(index,1);
    }
}

//constructor, is this more performant then a class?
function Employee(name,job,weeklyPay,age){
    this.name=name;
    this.job=job;
    this.weeklyPay=weeklyPay;
    this.age=age;
}
Employee.prototype.pay=function(){
    console.log(this.name + ": was paid " + weeklyPay);
}


function Manager(name,job,weeklyPay,age){
    Employee.call(this,name,job,weeklyPay,age);
    this.name=name;
    this.job=job;
    this.weeklyPay=weeklyPay;
    this.age=age;
    this.reports=[];
}
Manager.prototype=Object.create(Employee.prototype);

Manager.prototype.addReport=function(report){
    this.reports.push(report);
}
Manager.prototype.removeReport=function(index){
    this.reports.splice(index,1);
}

不,这不准确。

I'm assuming since classes in javascript are just syntax suger that class are being converted into constructor functions at some point

不是真的。两者都被转换为函数和对象的内部表示,但那是独立发生的。

I'm also assuming that his creates an extra thing for a engine to do?

不,"extra thing" 意味着 class 语法将被翻译成构造函数代码,然后像往常一样解析。这只是在您使用转译器时发生的情况,但肯定不是引擎的工作方式。

实际上,假设应该相反:class 语法更接近构造函数的内部表示,通过传达 intent 用作 class 它比需要额外启发式的普通 functions 更容易优化。它更具声明性(不需要任何突变)并且在规范中设计为更易于编译(例如通过暗示严格模式)。