* JavaScript クラス・オブジェクト [#ee1faea0]

*** オブジェクト生成 [#mc80828f]
 var obj = { name : "taro", 
            say  : function() { alert("HELLO!") } 
 };
*** 空のクラス定義 [#u48ec2eb]
 var Person = function() {};
*** コンストラクタ付きのクラス定義 [#lda4cc3f]
 var Person = function(name) {
     this.name = name;
     this.say = function() { alert(this.name); }
 };
*** インスタンス生成 [#m5dce2e9]
 var person = new Person("taro");
 person.say();
*** プロトタイプによるメソッド追加 [#n7668f55]
 Person.prototype = {
     say2: function() {
         alert("2: " + this.name);
 };
 person.say2();
*** 継承 [#w1db1e89]
 var Person = function(name) { this.name = name; };            // 親クラス
 Person.prototype = { say: function() { alert(this.name) }; };
 var Man = function(name) { this.name = "Mr." + name; };       // 子クラス
 Man.prototype = Person.prototype;                             // 継承
 
 var m = new Man("taro");
 m.say();
*** Prototype.jsを使って継承 [#q91482f9]
 var Woman = Class.create();                       // クラス定義
 Object.extend(Woman.prototype, Person.prototype); // 継承
 Object.extend(Woman.prototype,{                   // メソッドのオーバーライド
     initialize: function(name) {
                     this.name = "Miss." + name;
                 }
 });
 var w = new Woman("hanako");
 w.say();

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS