<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>タイトル</title> </head> <body> <section> <img src="example.png"> <p>リンクは<a href="https://foo.example.com/">こちら</a></p> </section> </body> </html>
const link = document.querySelector('a');
link.textContent = 'あちら';
link.href = 'https://bar.example.com';
Promise.any
// Before ES6 var a = 1; // After ES6 let b = 2; const c = 3
// Before ES6 // 1秒ごとにカウントダウンする(わざわざこんな書き方する必要はないのですが) function countDown(count, callback) { setTimeout(function() { console.log(count) callback(count - 1); },1000); } // 非同期で実行する処理がどんどん入れ子になる countDown(2, function(count){ countDown(count, function(count) { countDown(count, function(count) { console.log("finish!"); }); }); }); // Promise function countDown(count) { return new Promise(function(resolve, reject) { setTimeout(function() { console.log(count) resolve(count - 1); },1000); }); } // 入れ子にならずに書ける countDown(2) .then(countDown) .then(countDown) .then(function (count) { console.log("finish!"); });
// 非同期で実行する関数定義は同じ function countDown(count) { return new Promise(function(resolve, reject) { setTimeout(function() { console.log(count) resolve(count - 1); },1000); }); } // async/await Promiseより直感的に書ける async function asyncCall() { let count = await countDown(2); count = await countDown(count); count = await countDown(count); console.log("finish!"); } asyncCall();
// APIを呼び出した結果をconsoleログに出力 const url = 'https://foo.example.com/api' let request = new XMLHttpRequest(); request.onload = function() { console.log(request.response); }; request.open('GET', url); request.responseType = 'text'; request.send();
// APIを呼び出した結果をconsoleログに出力 const url = 'https://foo.example.com/api' $.ajax({ type: "GET", url: url, success: function(text){ console.log(text) } });
// APIを呼び出した結果をconsoleログに出力 const url = 'https://foo.example.com/api' async function callApi() { const res = await fetch(url); const text = await res.text(); console.log(text); } callApi();
JavaScriptに変換される
// typescript const foo: string = 'foo';
↓
// javascript "use strict"; var foo = 'foo';
静的型システム
JavaScriptのスーパーセット
開発者体験の向上
// javascript console.log(foo) // foo is not defined
// typescript console.log(foo) // Cannot find name 'foo'
ドキュメントの役割
// javascript function add(a, b) { return a + b }
function add(a: number, b: number): number { return a + b }
// 数値を2つ引数に受け取りその和を返す function add(a, b) { return a + b }
function add(a: string, b: number): number { return a + b } // Type 'string' is not assignable to type 'number'.
古いJavaScriptに変換してくれる
<script> export default { data() { return { greeting: 'Hello World!' } } } </script> <template> <p class="greeting">{{ greeting }}</p> </template> <style> .greeting { color: red; font-weight: bold; } </style>
import style from './style.css' const HelloWorld () => { const greeting = 'Hello World!!' return ( <div className="style.greeting"> {greeting} </div> ) }
.greeting { color: red; font-weight: bold; }
↑の方が実行速度が速く、実行コストもメンテコストも低い
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up