<!doctype html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<script src="site-common.js"></script>
<script src="addon.js"></script>
<script src="page.js"></script>
<title>test Document</title>
</head>
<body>
</body>
</html>
// site-common.js
// サイト内で共通で使う定数を纏めたオブジェクト
var constant = {
title:'とあるサンプル',
description:'RequireJSをつかってみるとあるサンプル'
};
// 指定されたidに対する定数を返す関数
function getConstant(id) {
return constant[id];
}
// 指定されたidに対する定数を追加定義する関数
function setConstant(id, value) {
constant[id] = value;
}
// 指定された文字をページ内に表示する関数
function commonPrint(str) {
var elem = document.createElement('p');
elem.innerHTML = str;
document.body.appendChild(elem);
}
// addon.js
// 鍵カッコを定義
constant.startSquare = '「';
constant.endSquare = '」';
// 鍵カッコをつけた文字列を返す
function addSquareBracket(str) {
return getConstant('startSquare') + str + getConstant('endSquare');
}
// page.js
// ページロード時にタイトルと説明を表示
window.addEventListener('load', function(){
var title = addSquareBracket(getConstant('title'));
var description = getConstant('description');
commonPrint(title);
commonPrint(description);
});