Javascript 編譯器:Traceur
Traceur 是一個來自 Google 的 Javascript 編譯器,通過它可以體驗一些很新并且有趣的 Javascript 語言特性,這些多數是還沒有被當前瀏覽器實現的 ECMAScript 標準或草案,比如:數組比較、類、模塊、迭代器、方法參數默認值、Promise等。
以下是幾個例子:
數組比較(Array Comprehension):
var array = [for (x of [0, 1, 2]) for (y of [0, 1, 2]) x + '' + y]; expect(array).to.be.eql([ '00', '01', '02', '10', '11', '12', '20', '21', '22' ]);
更精簡的函數定義語法(Arrow Functions):
var square = (x) => { return x * x; }; var square2 = x => x * x; var objectify = x => ({ value: x }); // Immediate return of an object literal must be wrapped in parentheses expect(square(4)).to.be.eql(16); expect(square2(4)).to.be.eql(16); expect(objectify(1)).to.be.eql({ value: 1 });
Promises:
function timeout(ms) { return new Promise((resolve) => { setTimeout(resolve, ms); }); } timeout(100).then(() => { console.log('done'); });
更詳細的語言特性列表,請參考:https://github.com/google/traceur-compiler/wiki/LanguageFeatures
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!