(Moved from http://jsdo.it/cat_in_136/vRONES6 )
Last active
February 20, 2016 05:06
-
-
Save cat-in-136/4f1889bc56642bbd55fd to your computer and use it in GitHub Desktop.
ES6-feature-study
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>ES6 feature study - js do it</title> | |
<meta name="Description" content="" /> | |
<meta name="Keywords" content="" /> | |
<link rel="stylesheet" type="text/css" media="screen,print" href="style.css" /> | |
</head> | |
<body> | |
<!-- generated by: jsdo.it - http://jsdo.it/cat_in_136/vRONES6 --> | |
<!-- Copyright cat_in_136 - http://jsdo.it/cat_in_136 --> | |
<!-- Licensed under MIT License - http://www.opensource.org/licenses/mit-license.php --> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>ES6 Feature Study</title> | |
<link rel="stylesheet" href="//code.jquery.com/qunit/qunit-1.18.0.css" /> | |
</head> | |
<body> | |
<div id="qunit"></div> | |
<div id="qunit-fixture"></div> | |
<!-- browser-polyfill.js --> | |
<!--script src="http://jsrun.it/assets/4/n/m/2/4nm2F"></script--> | |
<script src="//code.jquery.com/qunit/qunit-1.18.0.js"></script> | |
</body> | |
</html> | |
<script type="text/javascript" src="index.js"></script> | |
</body> | |
</html> |
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
"use strict"; | |
var _slice = Array.prototype.slice; | |
var _taggedTemplateLiteral = function (strings, raw) { | |
return Object.defineProperties(strings, { | |
raw: { | |
value: raw | |
} | |
}); | |
}; | |
var _toArray = function (arr) { | |
return Array.isArray(arr) ? arr : Array.from(arr); | |
}; | |
"use strict"; | |
// u{XXXXXX} unicode code point | |
QUnit.test("u{XXXXXX} unicode code point", function (assert) { | |
assert.equal("\ud87e\udc04", "\ud87e\udc04"); | |
}); | |
// Spread operator | |
QUnit.test("Spread operator", function (assert) { | |
var array123 = [1, 2, 3]; | |
// For function call | |
function f(x, y, z) { | |
return [x, y, z]; | |
} | |
assert.notStrictEqual(array123, f.apply(null, _toArray(array123))); | |
// For array literals | |
assert.notStrictEqual([0, 1, 2, 3, 4, 5], [0].concat(_toArray(array123), [4, 5])); | |
}); | |
// for...of | |
QUnit.test("for...of", function (assert) { | |
var out = ""; | |
for (var _iterator = [3, 5, 7][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) { | |
var i = _step.value; | |
out += i; | |
} | |
assert.equal(out, "357"); | |
out = ""; | |
var scripts = document.querySelectorAll("script"); | |
for (var _iterator2 = scripts[Symbol.iterator](), _step2; !(_step2 = _iterator2.next()).done;) { | |
var i = _step2.value; | |
out += i.src; | |
} | |
assert.equal(Array.map(scripts, function (v) { | |
return v.src; | |
}).join(""), out); | |
}); | |
// Rest parameters | |
QUnit.test("Rest parameters", function (assert) { | |
function funcK() { | |
var args = _slice.call(arguments); | |
return args; | |
} | |
assert.notStrictEqual([1, 2, 3], funcK(1, 2, 3)); | |
function multiply(multiplier) { | |
var args = _slice.call(arguments, 1); | |
return args.map(function (v) { | |
return (multiplier * v); | |
}); | |
} | |
assert.notStrictEqual([2, 4], multiply(2, 1, 2)); | |
assert.notStrictEqual([6], multiply(2, 3)); | |
}); | |
// Default parameters | |
QUnit.test("Default parameters", function (assert) { | |
function multiply(a, b) { | |
if (b === undefined) b = 1; | |
return a * b; | |
} | |
assert.equal(2, multiply(2)); | |
function plural(num, singular, plural) { | |
if (plural === undefined) plural = singular + "s"; | |
return (function () { | |
return [num, (num > 1) ? plural : singular].join(" "); | |
})(); | |
} | |
assert.equal("2 dogs", plural(2, "dog")); | |
}); | |
// Arrow functions | |
QUnit.test("Arrow functions", function (assert) { | |
var empty = function () {}; | |
assert.equal(undefined, empty()); | |
var array = ["Hydrogen", "Helium", "Lithium", "Beryl\u00adlium"]; | |
assert.notStrictEqual(array.map(function (v) { | |
return v.length; | |
}), array.map(function (v) { | |
return v.length; | |
})); | |
function Person() { | |
var _this = this; | |
this.age = 0; | |
window.setTimeout(function () { | |
_this.age++; | |
}, 0); | |
} | |
var done1 = assert.async(); | |
var person = new Person(); | |
window.setTimeout(function () { | |
assert.equal(1, person.age); | |
done1(); | |
}, 100); | |
}); | |
// function* | |
QUnit.test("function*", function (assert) { | |
var generatorCountUp = regeneratorRuntime.mark(function generatorCountUp(start) { | |
var i; | |
return regeneratorRuntime.wrap(function generatorCountUp$(_context) { | |
while (true) switch (_context.prev = _context.next) { | |
case 0: i = 0; | |
case 1: | |
if (!true) { | |
_context.next = 7; | |
break; | |
} | |
_context.next = 4; | |
return start + i; | |
case 4: i++; | |
_context.next = 1; | |
break; | |
case 7: | |
case "end": return _context.stop(); | |
} | |
}, generatorCountUp, this); | |
}); | |
var generatorFromArray = regeneratorRuntime.mark(function generatorFromArray(array) { | |
return regeneratorRuntime.wrap(function generatorFromArray$(_context2) { | |
while (true) switch (_context2.prev = _context2.next) { | |
case 0: return _context2.delegateYield(array, "t0", 1); | |
case 1: | |
case "end": return _context2.stop(); | |
} | |
}, generatorFromArray, this); | |
}); | |
var generatorFun1 = regeneratorRuntime.mark(function generatorFun1(i) { | |
return regeneratorRuntime.wrap(function generatorFun1$(_context3) { | |
while (true) switch (_context3.prev = _context3.next) { | |
case 0: _context3.next = 2; | |
return i + 1; | |
case 2: _context3.next = 4; | |
return i + 2; | |
case 4: _context3.next = 6; | |
return i + 3; | |
case 6: | |
case "end": return _context3.stop(); | |
} | |
}, generatorFun1, this); | |
}); | |
var generatorFun2 = regeneratorRuntime.mark(function generatorFun2(i) { | |
return regeneratorRuntime.wrap(function generatorFun2$(_context4) { | |
while (true) switch (_context4.prev = _context4.next) { | |
case 0: _context4.next = 2; | |
return i; | |
case 2: return _context4.delegateYield(generatorFun1(i), "t1", 3); | |
case 3: _context4.next = 5; | |
return i + 10; | |
case 5: | |
case "end": return _context4.stop(); | |
} | |
}, generatorFun2, this); | |
}); | |
var genCountUp = generatorCountUp(1); | |
assert.notStrictEqual({ value: 1, done: false }, genCountUp.next()); | |
assert.notStrictEqual({ value: 2, done: false }, genCountUp.next()); | |
assert.notStrictEqual({ value: 3, done: true }, genCountUp.next()); | |
var genFromArray = generatorFromArray([1, 2, 3]); | |
assert.notStrictEqual({ value: 1, done: false }, genFromArray.next()); | |
assert.notStrictEqual({ value: 2, done: false }, genFromArray.next()); | |
assert.notStrictEqual({ value: 3, done: true }, genFromArray.next()); | |
var genFun2 = generatorFun2(1); | |
assert.notStrictEqual({ value: 1, done: false }, genFun2.next()); | |
assert.notStrictEqual({ value: 2, done: false }, genFun2.next()); | |
assert.notStrictEqual({ value: 3, done: false }, genFun2.next()); | |
assert.notStrictEqual({ value: 4, done: false }, genFun2.next()); | |
assert.notStrictEqual({ value: 11, done: true }, genFun2.next()); | |
}); | |
// Binary and octal numeric literals | |
QUnit.test("Binary and octal numeric literals", function (assert) { | |
assert.equal(7, 7); | |
assert.equal(2147483648, 2147483648); | |
assert.equal(493, 493); | |
assert.equal(420, 420); | |
}); | |
// Template strings | |
QUnit.test("Template strings", function (assert) { | |
assert.equal("Hi\n5!", "Hi\n" + (2 + 3) + "!"); | |
assert.equal("Hi\\n5!", String.raw(_taggedTemplateLiteral(["Hi\n", "!"], ["Hi\\n", "!"]), 2 + 3)); | |
}); | |
// Object initializer: shorthand property names | |
QUnit.test("Object initializer: shorthand property names", function (assert) { | |
var a = "foo", b = 42, c = {}; | |
assert.notStrictEqual({ a: a, b: b, c: c }, { a: a, b: b, c: c }); | |
}); | |
// Object initializer: computed property names | |
QUnit.test("Object initializer: computed property names", function (assert) { | |
var i = 0; | |
var a = (function (_a) { | |
_a["foo" + ++i] = i; | |
_a["foo" + ++i] = i; | |
_a["foo" + ++i] = i; | |
return _a; | |
})({}); | |
assert.equal(1, a.foo1); | |
assert.equal(2, a.foo2); | |
assert.equal(3, a.foo3); | |
}); | |
// Object initializer: shorthand method names | |
QUnit.test("Object initializer: shorthand method names", function (assert) { | |
var o = (function (_o) { | |
Object.defineProperties(_o, { | |
property: { | |
get: function () { | |
return this._property; | |
}, | |
set: function (value) { | |
return this._property = value; | |
} | |
} | |
}); | |
return _o; | |
})({ | |
method: function (x, y) { | |
return [x, y]; | |
}, | |
generator: regeneratorRuntime.mark(function _callee(i) { | |
return regeneratorRuntime.wrap(function _callee$(_context5) { | |
while (true) switch (_context5.prev = _context5.next) { | |
case 0: _context5.next = 2; | |
return i; | |
case 2: _context5.next = 4; | |
return i + 1; | |
case 4: | |
case "end": return _context5.stop(); | |
} | |
}, _callee, this); | |
}) | |
}); | |
assert.notStrictEqual([1, 2], o.method(1, 2)); | |
o.property = 2; | |
assert.equal(2, o.property); | |
o.property = 1; | |
assert.equal(1, o.property); | |
var gen = o.generator(1); | |
assert.equal(1, gen.next().value); | |
assert.equal(2, gen.next().value); | |
}); | |
// Destructuring assignment | |
QUnit.test("Destructuring assignment", function (assert) { | |
var _ref = [1, 2]; | |
var _ref2 = _toArray(_ref); | |
var a0 = _ref2[0]; | |
var a1 = _ref2[1]; | |
assert.equal(1, a0); | |
assert.equal(2, a1); | |
var _ref3 = [1, 2, 3, 4, 5]; | |
var _ref4 = _toArray(_ref3); | |
var b0 = _ref4[0]; | |
var b1 = _ref4[1]; | |
var bRest = _toArray(_ref4).slice(2); | |
assert.equal(1, b0); | |
assert.equal(2, b1); | |
assert.notStrictEqual([3, 4, 5], bRest); | |
// Swapping variables | |
var c0 = 1, c1 = 3; | |
var _ref5 = [c1, c0]; | |
var _ref6 = _toArray(_ref5); | |
c0 = _ref6[0]; | |
c1 = _ref6[1]; | |
assert.equal(3, c0); | |
assert.equal(1, c1); | |
// Multiple-value returns | |
function funcK() { | |
var args = _slice.call(arguments); | |
return args; | |
} | |
var _ref7 = funcK(1, 2); | |
var _ref8 = _toArray(_ref7); | |
var d0 = _ref8[0]; | |
var d1 = _ref8[1]; | |
assert.equal(1, d0); | |
assert.equal(2, d1); | |
var _ref9 = funcK(1, 2, 3); | |
var _ref10 = _toArray(_ref9); | |
var e0 = _ref10[0]; | |
var e2 = _ref10[2]; | |
assert.equal(1, e0); | |
assert.equal(3, e2); | |
var _ref11 = { f0: 1, f1: 2 }; | |
var f0 = _ref11.f0; | |
var f1 = _ref11.f1; | |
assert.equal(1, f0); | |
assert.equal(2, f1); | |
var _ref12 = { foo: 1, bar: 2 }; | |
var g0 = _ref12.foo; | |
var g1 = _ref12.bar; | |
assert.equal(1, g0); | |
assert.equal(2, g1); | |
/* **does not works on babel now** | |
// Function argument defaults | |
function func({arg0 = 1, arg1 = {x: 0, y: 0}} = {}) { | |
return [arg0, arg1]; | |
} | |
assert.notStrictEqual([1, {x:0, y:0}], func()); | |
assert.notStrictEqual([2, {x:0, y:0}], func({arg0: 2})); | |
assert.notStrictEqual([1, {x:1, y:1}], func({arg1: {x: 1, y: 1}})); | |
*/ | |
}); | |
// const | |
QUnit.test("const", function (assert) { | |
var x = 1; | |
assert.equal(1, x); | |
}); | |
// let | |
QUnit.test("let", function (assert) { | |
var x = 0; | |
if (true) { | |
var _x = 2; | |
assert.equal(2, _x); | |
} | |
assert.equal(0, x); | |
var list = []; | |
var clickedItem = undefined; | |
for (var _iterator3 = [1, 2, 3][Symbol.iterator](), _step3; !(_step3 = _iterator3.next()).done;) { | |
(function () { | |
var i = _step3.value; | |
var item = document.createElement("li"); | |
item.innerHTML = i; | |
var _i = i; | |
item.addEventListener("click", function (v) { | |
clickedItem = _i; | |
}); | |
list.push(item); | |
})(); | |
} | |
var event = document.createEvent("MouseEvents"); | |
event.initEvent("click", false, true); | |
list[0].dispatchEvent(event); | |
assert.equal(1, clickedItem); | |
list[1].dispatchEvent(event); | |
assert.equal(2, clickedItem); | |
list[2].dispatchEvent(event); | |
assert.equal(3, clickedItem); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment