Coffeescript-classes-and-inheritance
CoffeeScript-クラスと継承
JavaScriptは class キーワードを提供しません。 オブジェクトとそのプロトタイプを使用して、JavaScriptの継承を実現できます。 すべてのオブジェクトには独自のプロトタイプがあり、プロトタイプから関数とプロパティを継承します。 プロトタイプもオブジェクトなので、独自のプロトタイプもあります。
プロトタイプ継承は従来の継承よりもはるかに強力ですが、初心者ユーザーにとっては難しく、混乱を招きます。
CoffeeScriptのクラス
この問題に対処するために、CoffeeScriptはJavaScriptのプロトタイプを使用して構築される class と呼ばれる基本構造を提供します。 以下に示すようにclassキーワードを使用して、CoffeeScriptでクラスを定義できます。
class Class_Name
例
次の例を考えてみましょう。ここでは、キーワード class を使用して Student という名前のクラスを作成しました。
class Student
上記のコードをコンパイルすると、次のJavaScriptが生成されます。
var Student;
Student = (function() {
function Student() {}
return Student;
})();
クラスのインスタンス化
次に示すように、他のオブジェクト指向プログラミング言語と同様に、new演算子を使用してクラスをインスタンス化できます。
new Class_Name
以下に示すように、 new 演算子を使用して、上記で作成した(Student)クラスをインスタンス化できます。
class Student
new Student
上記のコードをコンパイルすると、次のJavaScriptが生成されます。
var Student;
Student = (function() {
function Student() {}
return Student;
})();
new Student;
コンストラクターの定義
コンストラクターは、クラスをインスタンス化するときに呼び出される関数です。その主な目的は、インスタンス変数を初期化することです。 CoffeeScriptでは、次のように constructor という名前の関数を作成するだけで、コンストラクターを定義できます。
class Student
constructor: (name)->
@name = name
ここでは、コンストラクターを定義し、ローカル変数名をインスタンス変数に割り当てています。
*@* 演算子は *this* キーワードのエイリアスであり、クラスのインスタンス変数を指すために使用されます。
コンストラクタの引数の前に @ を配置すると、インスタンス変数として自動的に設定されます。 したがって、上記のコードは、以下に示すように簡単に書くことができます-
class Student
constructor: (@name)->
例
CoffeeScriptのコンストラクターの例を次に示します。 constructor_example.coffee という名前のファイルに保存します
#Defining a class
class Student
constructor: (@name)->
#instantiating a class by passing a string to constructor
student = new Student("Mohammed");
console.log "the name of the student is :"+student.name
コードのコンパイル
コマンドプロンプトを開き、次のように上記の例をコンパイルします。
c:\>coffee -c constructor_example.coffee
上記のコマンドを実行すると、次のJavaScriptが生成されます。
//Generated by CoffeeScript 1.10.0
(function() {
var Student, student;
Student = (function() {
function Student(name) {
this.name = name;
}
return Student;
})();
student = new Student("Mohammed");
console.log("The name of the student is :"+student.name);
}).call(this);
コードの実行
コマンドプロンプトで次のコマンドを実行して、上記の例を実行します。
coffee constructor_example.coffee
実行すると、上記の例では次の出力が得られます。
The name of the student is :Mohammed
インスタンスのプロパティ
オブジェクトと同じように、クラス内にプロパティを持つこともできます。 そして、これらは*インスタンスプロパティ*と呼ばれます。
例
次の例を考えてください。 ここでは、クラス内に変数(名前、年齢)と関数(message())を作成し、そのオブジェクトを使用してそれらにアクセスしました。 この例を instance_properties_example.coffee という名前のファイルに保存します
#Defining a class
class Student
name="Ravi"
age=24
message: ->
"Hello "+name+" how are you"
#instantiating a class by passing a string to constructor
student = new Student();
console.log student.message()
コンパイル時に、上記のコードは次の出力を生成します。
//Generated by CoffeeScript 1.10.0
(function() {
var Student, student;
Student = (function() {
var age, name;
function Student() {}
name = "Ravi";
age = 24;
Student.prototype.message = function() {
return "Hello " + name + " how are you";
};
return Student;
})();
student = new Student();
console.log(student.message());
}).call(this);
静的プロパティ
クラスで静的プロパティを定義できます。 静的プロパティの範囲はクラス内で制限されており、* thisキーワード*またはそのエイリアス @ シンボルを使用して静的関数を作成し、クラス名を_Class_Name.property_として使用してこれらのプロパティにアクセスする必要があります。
例
次の例では、messageという名前の静的関数を作成しました。 アクセスしました。 static_properties_example.coffee という名前のファイルに保存します。
#Defining a class
class Student
@message:(name) ->
"Hello "+name+" how are you"
console.log Student.message("Raju")
コマンドプロンプトを開き、次のコマンドを使用して上記のCoffeeScriptファイルをコンパイルします。
c:\>coffee -c static_properties_example.coffee
コンパイル時に、次のJavaScriptが提供されます。
//Generated by CoffeeScript 1.10.0
(function() {
var Student;
Student = (function() {
function Student() {}
Student.message = function(name) {
return "Hello " + name + " how are you";
};
return Student;
})();
console.log(Student.message("Raju"));
}).call(this);
以下に示すように、コマンドプロンプトで上記のcoffeeScriptを実行します。
c:\>coffee static_properties_example.coffee
実行すると、上記の例では次の出力が得られます。
Hello Raju how are you
継承
CoffeeScriptでは、 extends キーワードを使用して、あるクラスのプロパティを別のクラスに継承できます。
例
以下は、CoffeeScriptでの継承の例です。 ここには、 Add と My_class という2つのクラスがあります。 クラスMy_classのAddという名前のクラスのプロパティを継承し、 extends キーワードを使用してそれらにアクセスしました。
#Defining a class
class Add
a=20;b=30
addition:->
console.log "Sum of the two numbers is :"+(a+b)
class My_class extends Add
my_class = new My_class()
my_class.addition()
CoffeeScriptは、舞台裏でプロトタイプ継承を使用します。 CoffeeScriptでは、インスタンスを作成するたびに、親クラスのコンストラクターがオーバーライドされるまで呼び出されます。
以下の例に示すように、* super()*キーワードを使用して、サブクラスから親クラスのコンストラクターを呼び出すことができます。
#Defining a class
class Add
constructor:(@a,@b) ->
addition:=>
console.log "Sum of the two numbers is :"+(@a+@b)
class Mul extends Add
constructor:(@a,@b) ->
super(@a,@b)
multiplication:->
console.log "Product of the two numbers is :"+(@a*@b)
mul = new Mul(10,20)
mul.addition()
mul.multiplication()
動的クラス
CoffeeScriptはプロトタイプ継承を使用して、クラスのすべてのインスタンスプロパティを自動的に継承します。 これにより、クラスが動的になります。子が作成された後に親クラスにプロパティを追加しても、継承されたすべての子にプロパティが伝播されます。
class Animal
constructor: (@name) ->
class Parrot extends Animal
Animal::rip = true
parrot = new Parrot("Macaw")
console.log "This parrot is no more" if parrot.rip
実行時に、上記のCoffeeScriptは次のJavaScriptコードを生成します。
//Generated by CoffeeScript 1.10.0
(function() {
var Animal, Parrot, parrot,
extend = function(child, parent) { for (var key in parent) {
if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() {
this.constructor = child; } ctor.prototype = parent.prototype;
child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
Animal = (function() {
function Animal(name) {
this.name = name;
}
return Animal;
})();
Parrot = (function(superClass) {
extend(Parrot, superClass);
function Parrot() {
return Parrot.__super__.constructor.apply(this, arguments);
}
return Parrot;
})(Animal);
Animal.prototype.rip = true;
parrot = new Parrot("Macaw");
if (parrot.rip) {
console.log("This parrot is no more");
}
}).call(this);