Javasctiptのプロトタイプベースのオブジェクト指向を試みています。
もはや、いにしえの技術です。Babelなどを使ってクラスベースで書きましょう。
ちなみに、Babelを挟んでクラスベースで書いたものを訳すと以下のようになります。
javascript
1// 翻訳前
2class Bicycle{
3 constructor(args){
4 this.size = args.size;
5 }
6}
7
8class RoadBike extends Bicycle{
9 constructor(args){
10 super(args);
11 this.tape_color = args.tape_color;
12 }
13}
14
15// 翻訳後
16function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
17
18function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
19
20function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
21
22function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
23
24function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
25
26function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
27
28function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
29
30var Bicycle = function Bicycle(args) {
31 _classCallCheck(this, Bicycle);
32
33 this.size = args.size;
34};
35
36var RoadBike =
37/*#__PURE__*/
38function (_Bicycle) {
39 _inherits(RoadBike, _Bicycle);
40
41 function RoadBike(args) {
42 var _this;
43
44 _classCallCheck(this, RoadBike);
45
46 _this = _possibleConstructorReturn(this, _getPrototypeOf(RoadBike).call(this, args));
47 _this.tape_color = args.tape_color;
48 return _this;
49 }
50
51 return RoadBike;
52}(Bicycle);
new Bicycle
を実行するには引数が必要ですが、当然ながら元の状態の引数はわかりませんので、子クラスの宣言時にnew Bicycle
は行わず、Object.setPrototypeOf
でプロトタイプを設定しています。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/06 12:28