BabelでES6を始めるにあたってES6の基本文法とBabelがどのようにコンパイルするかを理解するためにまとめておきます。
Class
これまでnew functionなどを利用してclassを利用してきたがES6で他の言語と同等のclassが追加されました。
class Person{
//コンストラクタ
constructor(name) {
this.name = name;
}
//メソッド
say() {
return 'My Name is ' + this.name;
}
//静的メソッド
static say2(name) {
return '私の名前は' + name;
}
}
var taro = new Person('太郎');
console.log(taro.say());//My Name is 太郎
console.log(Person.say2("taro "));//私の名前はtaro
class Man extends Person{
isMan() {
return true;
}
}
class Woman extends Person{
isMan() {
return false;
}
}
var taro = new Man('太郎');
console.log(taro.isMan());//true
var hanako = new Woman('花子');
console.log(hanako.isMan());//false
↓
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
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 _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var Person = (function () {
//コンストラクタ
function Person(name) {
_classCallCheck(this, Person);
this.name = name;
}
//メソッド
_createClass(Person, [{
key: 'say',
value: function say() {
return 'My Name is ' + this.name;
}
//静的メソッド
}], [{
key: 'say2',
value: function say2(name) {
return '私の名前は' + name;
}
}]);
return Person;
})();
var taro = new Person('太郎');
console.log(taro.say()); //My Name is 太郎
console.log(Person.say2("taro ")); //私の名前はtaro
var Man = (function (_Person) {
_inherits(Man, _Person);
function Man() {
_classCallCheck(this, Man);
_get(Object.getPrototypeOf(Man.prototype), 'constructor', this).apply(this, arguments);
}
_createClass(Man, [{
key: 'isMan',
value: function isMan() {
return true;
}
}]);
return Man;
})(Person);
var Woman = (function (_Person2) {
_inherits(Woman, _Person2);
function Woman() {
_classCallCheck(this, Woman);
_get(Object.getPrototypeOf(Woman.prototype), 'constructor', this).apply(this, arguments);
}
_createClass(Woman, [{
key: 'isMan',
value: function isMan() {
return false;
}
}]);
return Woman;
})(Person);
var taro = new Man('太郎');
console.log(taro.isMan()); //true
var hanako = new Woman('花子');
console.log(hanako.isMan()); //false
このようにconstructorで簡単にコンストラクタの設定ができ、staticで静的メソッドが定義できます。
posted by ねこまんま at 19:23
|
Comment(0)
|
TrackBack(0)
|
関数/文法
|