gpt4 book ai didi

javascript - 为什么 TypeScript 不封装私有(private)字段?

转载 作者:搜寻专家 更新时间:2023-10-30 20:59:14 25 4
gpt4 key购买 nike

<分区>

为什么 TypeScript 不封装私有(private)变量?

给定以下 TypeScript:

    private engine: string;

constructor(engine: string) {
this.engine = engine;
}

start() {
console.log('Starting engine ' + this.engine);
}
}

var car = new Car("Fiat");
car.start();

当我编译时,我得到以下 JavaScript:

var Car = (function () {
function Car(engine) {
this.engine = engine;
}
Car.prototype.start = function () {
console.log('Starting engine ' + this.engine);
};
return Car;
}());
var car = new Car("Fiat");
car.start();

engine 变量是公开的。为什么 TypeScript 不产生更像这样的东西:

var Car = (function () {
var _engine;
function Car(engine) {
_engine = engine;
}
Car.prototype.start = function () {
console.log('Starting engine ' + _engine);
};
return Car;
}());

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com