開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- Node.js, Safari(JavaScript エンジン)
- Learning JavaScript [邦訳](参考書籍)
メタプログラミングRuby 第2版(Paolo Perrotta (著)、角 征典 (翻訳)、オライリージャパン)の1部(メタプログラミング Ruby)、1章(頭文字 M)、1.1(ゴーストタウンと市場)、1.2(メタプログラマのボブの物語)、1.2.1(ボブの最初の試み)を JavaScript で取り組んでみる。
HTML5
<button id="run0">run</button> <button id="clear0">clear</button> <pre id="output0"></pre> <script src="sample1.js"></script>
JavaScript
let btn0 = document.querySelector('#run0'),
btn1 = document.querySelector('#clear0'),
pre0 = document.querySelector('#output0');
let Greeting = (spec) => {
let that = {};
that.welcome = () => spec.text;
return that;
};
// 継承等
let Entity = (spec) => {
let that = {},
table = spec.table,
ident = spec.ident;
that.set = (col, val) => {
return `UPDATE ${table} SET ${col}='#{val}' WHERE id=${ident}`;
};
that.get = (col) => {
return `SELECT ${col} FROM ${table} WHERE id=${ident}`;
};
return that;
};
let Movie = (spec) => {
let that = Entity({table: 'movies', ident: spec.ident});
that.getTitle = () => that.get('title');
that.setTitle = (value) => that.set('title', value);
that.getDirector = () => that.get('director');
that.setDirector = (value) => that.set('director', value);
return that;
};
let output = () => {
pre0.textContent += `1.1\n`;
let g = Greeting({text:'Hello'});
pre0.textContent += g.welcome() + '\n';
pre0.textContent += `1.2.1\n`;
let movie = Movie({ident:1});
movie.setTitle('博士の異常な愛情');
movie.setDirector('スタンビー・キューブリック');
pre0.textContent +=
`${movie.getTitle()}\n` +
`${movie.getDirector()}\n`;
};
btn0.onclick = output;
btn1.onclick = () => {
pre0.textContent = '';
};
output();
0 コメント:
コメントを投稿