0

現在の壊れたコード: http://jsfiddle.net/9F52n/2/

私がやろうとしていること: クラスのように動作するオブジェクト/関数を定義する方法を学び、 static とインスタンス化可能な両方のサブクラスを定義できるようにします (以下の例ではシングルトン)。

現在、以下のコードはうまく機能しません。しかし、インスタンス化可能なクラスと静的クラスが削除された場合、クラス作成の非常に基本的なことがわかっていることがわかります。

だから、私の質問は次のとおりだと思います:TBRを定義した方法でネストされたクラス(シングルトンまたはその他)を定義する適切な/最もセマンティックな方法は何ですか?(function(){...})(window)

var TBR = (function() {
    // define local copy of taco bell run
    var TBR = function() {
        return new TBR.fn.init();
    },
        message = "hello world!";

    TBR.fn = TBR.prototype = {
        constructor: TBR,
        init: function() {
            console.log("From TBR Constructor: " + message);
        }
    }

    var InstantiatableClass = function() {
        return new TBR.InstantiatableClass, fn.init();
    }

   InstantiatableClass.fn =InstantiatableClass.prototype = {
        constructor: TBR.InstantiatableClass,
        init: function() {
            console.log("from InstantiatableClass: " + message);
        }
    }

    this.staticClass = function() {
        var subMessage = "little world";
        init = function() {
            console.log("from staticClass: " + subMessage);
        }
    }
    // expose TBR to the window object
    window.TBR = TBR;

})(window);
4

1 に答える 1

0
var InstantiatableClass = function() {
    return new TBR.InstantiatableClass, fn.init();
}

InstantiatableClass.fn =InstantiatableClass.prototype ...

これは動作しません。ローカル変数はオブジェクトを返しますが、プロトタイプはそれらに適用されませInstantiatableClassん。また、TBR.InstantiatableClassどこにも定義されていません。それがあなたが望んでいたものなら、あなたは使う必要があるでしょう

function InstantiatableClass() {
    // common constructor things
}
TBR.InstantiatableClass = InstantiatableClass; // assign a "static" property

また、プロトタイプを上書きする必要はありません。確かに、唯一の違いは、それconstructorが現在列挙可能であることです (忘れられない限り) が、以下ははるかにクリーンになります:

InstantiatableClass.fn = InstantiatableClass.prototype; // shortcut
InstantiatableClass.fn.init = function() { … };

ああ、jQuery のように機能するものが必要です。コンストラクター ( init) をプロトタイプのプロパティにするべきではありません。これは非常に奇妙であり、そうする理由がわかりません。このコードをお勧めします:

window.TBR = (function() {
    function TbrConstructor() {
        …
    }
    function InstantiableConstructor() {
        …
    }

    // Now, the creator functions:
    function TBR() { return new TbrConstructor; }
    function Instantiable() { return new InstantiableConstructor; }

    // Now, overwrite the "prototype" properties - this is needed for
    // (new TbrConstructor) instanceof TBR === true
    // and also create those fn shortcuts
    TBR.fn = TBR.prototype = TbrConstructor.prototype;
    Instantiable.fn = Instantiable.prototype = InstantiableConstructor.prototype;

    // we might overwrite the "constructor" properties like
    TBR.fn.constructor = TBR;
    // but I don't see much sense in that - they also have no semantic value

    // At last, make Instantiable a property on the TBR function object:
    TBR.Instantiable = Instantiable;
    // and then only
    return TBR;
})();

// Usage

TBR(); // returns a TbrConstructor instance
TBR.Instantiable(); // returns a InstantiableConstructor instance
于 2012-08-06T14:59:57.117 に答える