開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Safari、Firefox + Firebug (Webプラウザ、プラグイン)
- JavaScript (プログラミング言語)
- jQuery (JavaScript Library)
Head First JavaScript ―頭とからだで覚えるJavaScriptの基本( Michael Morrison (著), 豊福 剛 (翻訳)、オライリージャパン)の10章(カスタムオブジェクトを作成する)、自分で考えてみよう(p.473)を解いてみる。
その他参考書籍
自分で考えてみよう(p.473)
コード(BBEdit)
sample.js
var Blog = function (date, body) {
this.date = date || new Date();
this.body = body || 'Nothing going on today.';
},
blog = [new Blog(new Date('08/14/2008'), '注文'),
new Blog(new Date('08/19/2008'), '新しい'),
new Blog(new Date('08/16/2008'), '新しい'),
new Blog(new Date('08/12/2008'), 'ネット'),
new Blog()];
Date.prototype.shortFormat = function () {
return this.getMonth() + '/' + this.getDate() + '/' + this.getFullYear();
};
Blog.showBlog = function (n) {
var blog_html = '',
i,
hilight;
blog.sort(Blog.blogSorter);
if (!n) {
n = blog.length;
}
for (i = 0; i < n; i += 1) {
hilight = i % 2 == 0 ? true : false;
blog_html += blog[i].toHTML(hilight);
};
$('#d0').html(blog_html);
};
Blog.blogSorter = function (entry1, entry2) {
return entry2.date - entry1.date;
};
Blog.showSignature = function () {
return 'This blog created by ' + Blog.prototype.signature;
};
Blog.prototype.toHTML = function (highlight) {
var blog_html = highlight ? '<p style="background-color: #EEEEEE;">' :
'<p>';
blog_html += '<strong>' + this.date.shortFormat() + '</strong><br />' +
this.body + '<br /><em>' + Blog.showSignature() + '</em></p>';
return blog_html;
};
Blog.prototype.signature = 'Puzzler Ruby';
Blog.showBlog();
0 コメント:
コメントを投稿