Javascirptでクラスという言葉を使っていいものかどうか知りませんが、便宜上そう呼びます。
とりあえず自分なり調べた限りのまとめです。
// 親クラス
var Parent = function(){}
Parent.prototype = {
speak: function () {
alert("I'm " + this.name);
}
};
// 子クラス1
var Child1 = function () {
this.name = 'child1';
}
Child1.prototype = $.extend({
kick: function () {
alert("Kick!!");
}
}, Parent.prototype);
// 子クラス 2
var Child2 = function () {
this.name = 'child2';
}
Child2.prototype = $.extend({
punch: function () {
alert("Punch!!");
}
}, Parent.prototype);
var c1 = new Child1();
c1.speak(); // alert("I'm child1")
c1.kick(); // alert("Kick!!")
var c2 = new Child2();
c2.speak(); // alert("I'm child2")
c2.punch(); // alert("Punch!!")
prototypeをマージするイメージでしょうか。
$.extend の引数の順番が親クラス -> 子クラス、ではなくて 子クラス -> 親クラスとなります。
多重継承も可能なようですが試していません。
jQuery.extend( [deep], target, object1, [objectN] )
http://docs.jquery.com/Utilities/jQuery.extend


0 コメント:
コメントを投稿