JsOOP

この記事では、 従来のOOPとJavaScriptの厳密 な型指定 (<2.0)のシミュレーションに焦点を当てます



JsOOP- オブジェクト指向スタイルでJavaScriptコードを記述するためのミニフレームワークについて説明します



OOPから始めましょう。 JavaScriptにクラスの概念を導入するための多くのオプションがあります。 それらをすべてリストするのではなく、すぐにJsOOPを使用してクラスの定義に進むことを提案します。



var Zoo = Zoo || {};



Zoo.Animal = Class

({

    construct: function(name)

    {

        this._name = name;

    },



    methods:

    {

        Scream: null,



        SayName: function()

        {

            document.write(Zoo.Animal.QuoteName(this._name));

        }

    },



    statics:

    {

        QuoteName: function(name)

        {

            return "{ " + name + " }";

        }

    }

})




* This source code was highlighted with Source Code Highlighter.






(namespace) Zoo. Animal :

construct – -, methods – instance- (Scream – ), statics – .



, (private protected) JsOOP . .



:



Zoo.Dog = Class

({

    base: Zoo.Animal,



    construct: function(name, bark, loudness)

    {

        this.$base()(name);

        this._bark = bark;

        this._loudness = loudness;

    },



    methods:

    {

        Scream: function()

        {

            var s = "";

            for (var i = 0; i < this._loudness; i++)

            {

                s += this._bark;

            }

            document.write(s);

        },



        SayName: function()

        {

            document.write(this._bark + " ");

            this.$base('SayName')();

        }

    }

})




* This source code was highlighted with Source Code Highlighter.






.



:



var bob = new Zoo.Dog("Bob", "Woof!", 3);



bob.SayName(); // "Woof!Woof! { Bob }"

bob.Scream(); // "Woof!Woof!Woof!"




* This source code was highlighted with Source Code Highlighter.






, vars. , Dog:



Zoo.Dog = Class

({

    // ...



    vars:

    {

        _bark: Type.String,

        _loudness: Type.Number

    }



    // ...

})




* This source code was highlighted with Source Code Highlighter.






Type . , :



Type.String = function(v)

{

    return typeof (v) == "string";

}




* This source code was highlighted with Source Code Highlighter.






, , Type.Interface, , . , :



vars:

{

    _animal: Type.Interface(['SayName', 'Scream'])

}




* This source code was highlighted with Source Code Highlighter.






JsOOP . :



oo_debug.js – ( Class) vars- .



oo.js – , , . vars .



types.js – -.






All Articles