私はクラス内でしか使用できず、クラスの外でアクセスできないようにする関数を持っています。それは私が生命維持内側に包まれた以外、グローバル関数を汚染として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 {
...
}
を関数を宣言された。しかし、これは良いではありません。では、どういうわけかES6クラス内でローカル関数を作成できますか?
const localFunc = Symbol();
class Auth {
/*@ngInject*/
constructor($http, $cookies, $q, User) {
this.$http = $http;
this.$cookies = $cookies;
this.$q = $q;
this.User = User;
this[localFunc] = function() {
// I am private
};
}
}
ES6モジュールローダーを使用している場合、クラス外の関数を宣言してもグローバルスコープは汚染されません。それは、ES6でプライベートな機能を確保するために行く方法です – CodingIntrigue
@MichałPerłakowski私はそれが重複しているとは思わない、[提案された標準](https://stackoverflow.com/questions/22156326/private-properties-in-javascript-es6 -classes)は、*ステートフルなインスタンス固有のプロパティを扱います*関数/メソッドではありません。 – Bergi