前回に続き、今回はjasmineのsrc配下に入っているPlayer.jsとSong.jsを見てみた。
Player.js
Player.js
function Player() {
}
Player.prototype.play = function(song) {
this.currentlyPlayingSong = song;
this.isPlaying = true;
};
Player.prototype.pause = function() {
this.isPlaying = false;
};
Player.prototype.resume = function() {
if (this.isPlaying) {
throw new Error("song is already playing");
}
this.isPlaying = true;
};
Player.prototype.makeFavorite = function() {
this.currentlyPlayingSong.persistFavoriteStatus(true);
};
-
function Player()
- 関数Player()を定義
- からっぽ!
-
Player.prototype.play = function(song)
- prototype.playを代入先にして引数songを持つ無名関数を定義
- this.currentlyPlayingSong(現在再生中Song)に引数songを代入
- this.isPlaying(再生中?)にtrue(再生中)を代入
-
Player.prototype.pause = function()
- prototype.pauseを代入先にして引数を持たない無名関数を定義
- this.isPlaying(再生中?)にfalse(非再生中)を代入
-
Player.prototype.resume = function()
- prototype.resumeを代入先にして引数を持たない無名関数を定義
- if (this.isPlaying)(もし再生中なら)
- throw new Error("song is already playing");新しい例外をメッセージつきでスロー。
- this.isPlaying(再生中?)にtrue(再生中)を代入
- 最初よくわからなかったけど、再起動したときに音楽を再生する(再起動前に音楽を再生していた)というtrueなのかな。
-
Player.prototype.makeFavorite = function()
- prototype.makeFavoriteを代入先にして引数を持たない無名関数を定義
- currentlyPlayingSong(現在再生中の音楽)
- persistFavoriteStatus(好きなステータスを永続化)
- なので、currentlyPlayingSong.persistFavoriteStatusは(現在再生中の音楽をお気に入りに登録)的な意味なのかな。それにtrue(イエス!)を代入。
こんな感じの解釈でいいんでしたっけ。
java script自体が久々なのでだいぶ忘れてるなぁ。
たしかprototypeっていうのが言語としてデフォルトで用意されていて、親子関係でさかのぼれるんでしたよね。prototypeが関数オブジェクトなんでしたっけ。
Song.js
Song.js
function Song() {
}
Song.prototype.persistFavoriteStatus = function(value) {
// something complicated
throw new Error("not yet implemented");
};
-
function Song()
- 関数Song()を定義
- からっぽ!
-
Song.prototype.persistFavoriteStatus = function()
- persistFavoriteStatusは(続きますお気に入り状態)
- prototype.persistFavoriteStatusを代入先にして引数valueを持つ無名関数を定義
- something complicatedは(複雑な何か)だと思うのでここにややこしい業務処理があるとして。みたいなニュアンスなのかな。
- "not yet implemented"は(まだ実装されていません)なのでたぶんそういう意味ですね。
- throw new Error("not yet implemented");新しい例外をメッセージつきでスロー。
Songのほうは関数が1つ。シンプルです。(なにせ実装してないのでシンプルです)
テストコード観てみないとこれだけだとわからないですね
次回はテストコードを見てみます。
それにしてもjava script本当に久しぶりだなぁ。