More Related Content
Similar to VSハッカソン TypeScript ハンズオン
Similar to VSハッカソン TypeScript ハンズオン (20)
VSハッカソン TypeScript ハンズオン
- 20. モジュール化しよう
• 「sample.ts」を以下のように変更します。
module App{
var button:HTMLButtonElement =
<HTMLButtonElement>document.getElementById("exec");
var textbox:HTMLInputElement =
<HTMLInputElement>document.getElementById("message");
button.addEventListener("click", function(){
alert(textbox.value);
}, false);
}
- 23. プロパティを追加しよう
• 「 message.ts」を以下のように変更します。
module App{
export class Message{
private _value: String;
constructor(value: String){
this._value = value;
}
public get value(): String{
return this._value;
}
public set value(v: String){
this._value = v;
}
}
}
- 26. ファイルを参照しよう
• 「sample.ts」から「message.ts」を参照します。
• 「sample.ts」を以下のように変更します。
• ※画面は動かなくなります。
/// <reference path="message.ts" />
module App{
var button:HTMLButtonElement =
<HTMLButtonElement>document.getElementById("exec");
var textbox:HTMLInputElement =
<HTMLInputElement>document.getElementById("message");
button.addEventListener("click", function(){
var msg:App.Message = new App.Message(textbox.value);
alert(msg.value.toString());
}, false);
}
- 28. jQuery を参照しよう
• 「sample.html」にjQueryの参照を追加します。
• 以下のようなCDNから取ってくると便利です。
• http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.min.js
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div>
<input type="text" id="message" />
<button type="button" id="exec">Click</button>
</div>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.min.js"></script>
<script src="scripts/message.js"></script>
<script src="scripts/sample.js"></script>
</body>
</html>
- 30. $を解決しよう
• 「sample.ts」を以下のように変更しまて、コンパイルします。
/// <reference path="message.ts" />
declare var $: any;
module App{
var button:any = $("#exec");
var textbox:any = $("#message");
button.on("click", function(){
var msg:App.Message = new App.Message(textbox.val());
alert(msg.value.toString());
});
}