0

私は、適切な名前間隔で JavaScript モジュール パターンを練習してきました。したがって、基本的には名前空間を宣言し、各名前空間にはカプセル化された特定のモジュールがあります。ここまで書いてきた内容です。コードは適切にコメントされています。

// namespace has been defined somewhere not to worry that it will be undefined
if (NAMESPACE == null || typeof (NAMESPACE) == 'undefined') {
    NAMESPACE = {};

    var id = function (id) {
        var _all_ids = {};
        var persona = {};
        var _id = id; // _id is a local variable that stores the argument

        var getId = function () { //this function returns the local private variable
            return _id;
        }

        persona.getId = getId;
        var _closed = false;

        var close = function () {
            delete _all_ids[getId()];
            this._closed = true;
        }

        persona.close = close;

        return persona; // persona is an object that has two properties `getId` and `close`. Both these are functiona
    }

    NAMESPACE['id'] = id; // so basically this will become NAMESPACE.id.getId or NAMESPACE.id.close
} 

誰でも理解できるように、このコードに完全にコメントしました。単純な名前空間を宣言し、その中にモジュールを追加します。もちろん、モジュールはカプセル化を使用しています。

私のインストラクターの 1 人は、このコードには基本的な欠陥の基準があるかどうかを示唆しています。コードは正常に実行されますが、これを理解できません。

それは標準的な賢明さで十分ですか?それとも私はそれを完全に間違っていますか?

4

4 に答える 4

0

関数を名前空間内に配置しないのはなぜですか?

NAMESPACE = {
 id: function (id) {
  // The id function in here
 }
}
于 2015-08-27T14:37:37.790 に答える