開発環境
- 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.477-479)を解いてみる。
その他参考書籍
自分で考えてみよう(p.477-479)
コード(BBEdit)
sample.js
var Blog = function (date, body, image) {
this.date = date || new Date();
this.body = body || 'Nothing going on today';
this.image = image;
},
div = $('#d0'),
blog = [new Blog(null, null,
'https://lh5.googleusercontent.com/' +
'rljVgO2PsdWyf5TVbjD3xefkzEDGpXR5QVeAKaPnJlI=s222-p-no'),
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'), 'ネット')];
Date.prototype.shortFormat = function () {
return this.getMonth() + '/' + this.getDate() + '/' + this.getFullYear();
};
Blog.showBlog = function (n) {
var blog_html = '',
i,
highlight = true;
blog.sort(Blog.blogSorter);
if (!n) {
n = blog.length;
}
for (i = 0; i < n; i += 1) {
blog_html += blog[i].toHTML(highlight);
highlight = !highlight;
};
$('#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 ? '<div style="background-color: #EEEEEE;">' :
'<div>';
blog_html += '<strong>' + this.date.shortFormat() + '</strong><br />';
if (this.image) {
blog_html += '<table><tr><td><img src="' + this.image + '" /></td>' +
'<td style="vertical-align:top">' + this.body + '</td></tr>' +
'</table>';
} else {
blog_html += this.body + '<br />';
}
blog_html += '<em>' + Blog.showSignature() + '</em></div><br />';
return blog_html;
};
Blog.prototype.signature = 'Puzzler Ruby';
Blog.showBlog();
0 コメント:
コメントを投稿