Skip to content

Instantly share code, notes, and snippets.

@kenmori
Last active September 2, 2015 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenmori/d32f1f5a3a61a81c57c9 to your computer and use it in GitHub Desktop.
Save kenmori/d32f1f5a3a61a81c57c9 to your computer and use it in GitHub Desktop.
ES6マラソン・1 〜BabelでES6(Class,let,const,DefaultParameter,Destructuring)がどのようにトランスパイルされるか試して理解する〜 ref: http://qiita.com/M-ISO/items/b3da7913ca0f2140201e
function get(firstName = 'morita', lastName = 'kenji'){
}
//ex1 babel
function today() { return { d: 6, m: 2, y: 2013 }; }
var { m: month, y: year } = today(); // month = 2, year = 2013
//ex1 after
"use strict";
function today() {
return { d: 6, m: 2, y: 2013 };
}
var _today = today();//関数名が「_変数」になっている
var month = _today.m;//オブジェクトプロパティを参照代入している
var year = _today.y;
// month = 2, year = 2013
//ex2 babel
let obj = { first: 'Jane', last: 'Doe' };
let {first:i,last:s} = obj;//オブジェクトをmatchするところに割り当てている
//ex2 after
'use strict';
var obj = { first: 'Jane', last: 'Doe' };
var i = obj.first;
var s = obj.last;
console.log(i)//Jane
console.log(s)//Doe
class morita {
constructor(firstName){
this.firstName = firstName;
this.lastName = 'kenji';
this.get = ()=>{
return this.firstName + this.lastName;
}
}
}
let firstName = new morita('morita');
firstName.get()//moritakenji
//ex1
let [x] = ["morita"];
console.log([x]);//['morita']
//ex2
let [x] = ["morita"];
console.log(x);//morita
//ex3
var [a,,b] = [1,2,3];
console.log(b)//3
//ex3 after
"use strict";
var _ref = [1, 2, 3];//_refという変数に格納される
var a = _ref[0];
var b = _ref[2];//commaを含めて何番目か[(0),(1),(2)]の[2]
console.log(b);//3
//ex4
var [a,,,,,b] = [1,2,3]; //commaの数増やす
console.log(b)//undefined //[1,2,3,undefined,undefined(ココ)]ココを参照しようとしている
//ex4 after
"use strict";
var _ref = [1, 2, 3];
var a = _ref[0];
var b = _ref[5];//5番目にbが割り当てられている
console.log(b);//undefined
//ex5
let [,,,y] = ['a','b','c','d','e']//
//ex5 after
'use strict';
var _ref = ['a', 'b', 'c', 'd', 'e'];
var y = _ref[3];
y//3
//ex1 babel
function g({name: x}) {
console.log(x);
}
g({name: 5})//そのまま渡している
//ex1 after
"use strict";
function g(_ref) {
var x = _ref.name;
console.log(x);
}
g({ name: 5 });//5
function _classCallCheck(instance, Constructor){
if (!(instance instanceof Constructor))
{ throw new TypeError('Cannot call a class as a function'); }
}
...
_classCallCheck(this, morita);
'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var morita = function morita(firstName) {
var _this = this;
_classCallCheck(this, morita);
this.firstName = firstName;
this.lastName = 'kenji';
this.get = function () {
return _this.firstName + _this.lastName;
};
};
var firstName = new morita('morita');
firstName.get();
'use strict';
function get() {
var firstName = arguments.length <= 0 || arguments[0] === undefined ? 'morita' : arguments[0];
var lastName = arguments.length <= 1 || arguments[1] === undefined ? 'kenji' : arguments[1];
}
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
'use strict';
function morita() {
var x = undefined;
x;
x = 'kenji';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment