ファイル構成:
/lib/math.js
/app.js
コード
/lib/math.js
// 加算関数
exports.add = function(a, b)
{
return a + b;
}
// sayhelo関数
exports.sayhelo = function()
{
switch (arguments.length)
{
case 1:
return 'helo ' + arguments[0] + '.';
default:
return 'helo everyone.';
}
}
/app.js
// 関数を独立なファイルに定義し、ここからオブジェクトとして呼び出す
var maths = require('lib/math');
var self = Ti.UI.createWindow(
{
backgroundColor : '#cccccc',
navBarHidden : true,
exitOnClose : true
});
var button1 = Ti.UI.createButton(
{
height : 44,
width : 200,
title : ('Titanium require sample!'),
left : 10,
top : 10
});
self.add(button1);
button1.addEventListener('click', function()
{
func1();
});
self.open();
var func1 = function()
{
// 各関数の呼び出し
alert(maths.add(1, 1) + '\n' + maths.sayhelo() + '\n' + maths.sayhelo('koma'));
};
実行結果

0 件のコメント:
コメントを投稿