0

私はES6で実験しています。特に、クラスと継承。クラスAppleでは、拡張しPolygonます。Polygonのメソッドを拡張sayName()して、console.log に出力したい。

traceur で実行すると、次のようになりますundefinedconsole.log(foo);

class Polygon {
  constructor(height, width) { //class constructor
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }

  sayName() { //class method
    return 'Hi, I am a', this.name + '.';
  }
}


class Apple extends Polygon {
    constructor(length) {
    super(length, length); //call the parent method with super
    this.name = 'apple';
  }

  sayName() {
    var foo = super();
    console.log(foo);
  }
}


let d = new Apple(5);
d.sayName();

トレーサー:

System.register("class", [], function() {
  "use strict";
  var __moduleName = "class";
  function require(path) {
    return $traceurRuntime.require("class", path);
  }
  var Polygon = function Polygon(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  };
  ($traceurRuntime.createClass)(Polygon, {sayName: function() {
      return 'Hi, I am a', this.name + '.';
    }}, {});
  var Apple = function Apple(length) {
    $traceurRuntime.superConstructor($Apple).call(this, length, length);
    this.name = 'apple';
  };
  var $Apple = Apple;
  ($traceurRuntime.createClass)(Apple, {sayName: function() {
      var foo = $traceurRuntime.superConstructor($Apple).call(this);
      console.log(foo);
    }}, {}, Polygon);
  var d = new Apple(5);
  d.sayName();
  return {};
});
System.get("class" + '');
  1. どうすればクラスで優秀になり、sayName()ショーを価値あるものにすることができますか?Appleconsole.log(foo)
  2. traceur がコンパイルされたコードを表示すると思っていましたが、そうではありませんでした。たとえば、$traceurRuntime.createClass()これらのコンストラクターがどのように作成されているかを理解するのに役立ちません。コンパイルされたコードを表示するために traceur を間違って使用していませんか?
4

1 に答える 1

1

super呼び出し元のメソッドではなく、クラス/コンストラクターを参照します。したがって、親関数を 内から呼び出したい場合は、次のsayName()ように記述する必要があります。

sayName() {
    var foo = super.sayName();
    console.log(foo);
}
于 2015-01-12T11:11:05.197 に答える