Last active
November 5, 2018 00:11
-
-
Save dzharii/82ec25fd068a4375f753eb2736c47d55 to your computer and use it in GitHub Desktop.
BabelVsTypescriptCmp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://babeljs.io/en/repl | |
// http://www.typescriptlang.org/play/ | |
// 001 Code | |
let x = [1, 2, 3]; | |
if (typeof x === 'object') { | |
} | |
// 001 Babel | |
'use strict'; | |
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } | |
: function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? | |
"symbol" : typeof obj; }; | |
var x = [1, 2, 3]; | |
if ((typeof x === 'undefined' ? 'undefined' : _typeof(x)) === 'object') {} | |
// 001 TypeScript | |
var x = [1, 2, 3]; | |
if (typeof x === 'object') { | |
} | |
// ================================================================================================================================ | |
// 002 Code: | |
let x = [1, 2, 3]; | |
for (y of x) { | |
} | |
// 002 BBL: | |
"use strict"; | |
var x = [1, 2, 3]; | |
var _iteratorNormalCompletion = true; | |
var _didIteratorError = false; | |
var _iteratorError = undefined; | |
try { | |
for (var _iterator = x[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); | |
_iteratorNormalCompletion = true) { | |
y = _step.value; | |
} | |
} catch (err) { | |
_didIteratorError = true; | |
_iteratorError = err; | |
} finally { | |
try { | |
if (!_iteratorNormalCompletion && _iterator.return) { | |
_iterator.return(); | |
} | |
} finally { | |
if (_didIteratorError) { | |
throw _iteratorError; | |
} | |
} | |
} | |
// 002 TS: | |
var x = [1, 2, 3]; | |
for (var _i = 0, x_1 = x; _i < x_1.length; _i++) { | |
y = x_1[_i]; | |
} | |
// ================================================================================================================================ | |
// 003 Code: | |
class A { | |
sayHello() { | |
} | |
} | |
// 003 BBL: | |
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) | |
{ var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; | |
if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | |
return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); | |
if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { | |
throw new TypeError("Cannot call a class as a function"); } } | |
var A = function () { | |
function A() { | |
_classCallCheck(this, A); | |
} | |
_createClass(A, [{ | |
key: "sayHello", | |
value: function sayHello() {} | |
}]); | |
return A; | |
}(); | |
// 003 TS: | |
var A = /** @class */ (function () { | |
function A() { | |
} | |
A.prototype.sayHello = function () { | |
}; | |
return A; | |
}()); | |
// ================================================================================================================================ | |
// 004 Code (Extended Parameter Handling: Default Parameter Values) | |
function f (x, y = 7, z = 42) { | |
return x + y + z | |
} | |
f(1) === 50 | |
// 004 BBL: | |
function f(x) { | |
var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 7; | |
var z = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 42; | |
return x + y + z; | |
} | |
f(1) === 50; | |
// 004 TS | |
function f(x, y, z) { | |
if (y === void 0) { y = 7; } | |
if (z === void 0) { z = 42; } | |
return x + y + z; | |
} | |
f(1) === 50; | |
// ================================================================================================================================ | |
// 005 Enhanced Object Properties: Computed Property Names | |
function quux() { | |
return 42; | |
} | |
let obj = { | |
foo: "bar", | |
[ "baz" + quux() ]: 42 | |
} | |
// 005 BBL: | |
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, | |
{ value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | |
function quux() { | |
return 42; | |
} | |
var obj = _defineProperty({ | |
foo: "bar" | |
}, "baz" + quux(), 42); | |
// 005 TS: | |
var _a; | |
function quux() { | |
return 42; | |
} | |
var obj = (_a = { | |
foo: "bar" | |
}, | |
_a["baz" + quux()] = 42, | |
_a); | |
// ================================================================================================================================ | |
// 006 Destructuring Assignment: Array Matching | |
var list = [ 1, 2, 3 ] | |
var [ a, , b ] = list | |
[ b, a ] = [ a, b ] | |
// 006 BBL: | |
var _slicedToArray = function () { function sliceIterator(arr, i) { | |
var _arr = []; var _n = true; var _d = false; var _e = undefined; | |
try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); | |
if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } | |
finally { try { if (!_n && _i["return"]) _i["return"](); } | |
finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { | |
if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } | |
else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | |
var list = [1, 2, 3]; | |
var _list = list[(b, a)] = [a, b], | |
_list2 = _slicedToArray(_list, 3), | |
a = _list2[0], | |
b = _list2[2]; | |
// 006 TS: | |
var list = [1, 2, 3]; | |
var _a = list[b, a] = [a, b], a = _a[0], b = _a[2]; | |
// ================================================================================================================================ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment