我有一個函數只能在一個類中使用,並且不希望它可以在類之外訪問。如何在ES6類中聲明本地函數?
class Auth {
/*@ngInject*/
constructor($http, $cookies, $q, User) {
this.$http = $http;
this.$cookies = $cookies;
this.$q = $q;
this.User = User;
localFunc(); // Need to create this function, and need it to be accessible only inside this class
}
}
我所做到目前爲止被宣告類
function localFunc() {
return 'foo';
}
class Auth {
...
}
外的函數。然而,這是它污染了全球功能並不好,但我裹着這裏面IIFE。那麼,有沒有辦法在ES6類中創建一個本地函數?
如果您使用的是ES6模塊加載器,則在該類之外聲明該函數將不會污染全局範圍。這是確保私有函數在ES6 – CodingIntrigue
@MichałPerłakowski我不認爲它是重複的,[建議規範](https://stackoverflow.com/questions/22156326/private-properties-in-javascript-es6類)處理*有狀態的,特定於實例的屬性*不是函數/方法。 – Bergi