これは角度2のプロジェクトです。私はそれを使用して別のクラスを持っているgetterメソッドで返されるプライベートTypeScriptクラスの変数は、他のクラスでも影響を受けますか?
export class CityService {
private availableCities: City[] = [];
constructor() {
this.availableCities.push(new City('london', 'London'));
this.availableCities.push(new City('paris', 'Paris'));
}
getAll() {
return this.availableCities;
};
}
:
私は、静的な都市データでクラスを持っている
export class NavbarComponent {
private cities: City[];
private currentCity : City;
constructor(private cityService: CityService) {
this.cities = cityService.getAll();
this.currentCity = cityService.getCurrent();
this.cities.forEach((city, index) => {
if (city.name == this.currentCity.name) {
//@todo BUG This affects private cities variable in CityService class WTF!!!!!!!
this.cities.splice(index, 1);
return;
}
})
}
}
だから私はのgetAll持つすべての都市をロードしようとしている、このクラスのコンストラクタで() 方法。これらの都市は、NavbarComponent独自のprivate cities変数にロードされます。しかし、NavbarComponentの都市で操作すると、CityServiceの都市のプライベートプロパティが変更されます。 なぜ????????それは世紀の私の認識を台無しにする!
誰も説明し、それはあなたがその都市の参照を返した
活字版2.1.5
ここでは何も変わっていません。配列の参照を渡して、その後にその配列を変更しています。何が起こると思いますか? –