0
// Options: --annotations --array-comprehension --async-functions --debug --debug-names --exponentiation --free-variable-checker --generator-comprehension --low-resolution-source-map --input-source-map --member-variables --module-name --referrer --require --script --symbols --types --validate 

//annotation class
class Template{
    value:string;
    constructor(value:string){
        this.value = value;
    }
}

//annotation class
class Attribute{}

@Template('<div>xxx</div>')
class SomeEl{
    @Attribute counter:int=0;
    constructor(){}
}


(function main(){
    console.log(SomeEl.annotations);
    console.log(SomeEl.properties); //prints undefined
})();

atscriptでフィールド注釈にアクセスするにはどうすればよいですか? 私はクラスの注釈にしかアクセスできませんが、クラス内のフィールドの注釈にはアクセスできません。助けていただければ幸いです

上記はトランスコードされます

$traceurRuntime.options.symbols = true;
var Template = function Template(value) {
  "use strict";
  this.value = value;
};
($traceurRuntime.createClass)(Template, {}, {});
Object.defineProperty(Template, "parameters", {get: function() {
    return [[$traceurRuntime.type.string]];
  }});
var Attribute = function Attribute() {
  "use strict";
};
($traceurRuntime.createClass)(Attribute, {}, {});
var SomeEl = function SomeEl() {
  "use strict";
  this.counter = 0;
};
($traceurRuntime.createClass)(SomeEl, {}, {});
Object.defineProperty(SomeEl, "annotations", {get: function() {
    return [new Template('<div>xxx</div>')];
  }});
(function main() {
  console.log(SomeEl.annotations);
  console.log(SomeEl.properties);
})();

@Attributeそして、それがファイルの注釈と見なされているとは思いませんcounter

4

1 に答える 1

1

ここでうまくいかないことがいくつかあります。コンストラクターの注釈は、クラスの注釈と同じです。

@Template('<div>xxx</div>')
class SomeEl{
   @Attribute
   constructor(){}
}

上記は次のように変換されます。

Object.defineProperty(SomeEl, "annotations", {get: function() {
  return [new Template('<div>xxx</div>'), new Attribute];
}});

コンストラクターの注釈は、クラスの注釈と同じであることに注意してください。他の関数の注釈は、その関数に注釈を配置しますが、コンストラクターとクラスは本質的に同じです。

注釈に表示されなかった理由new Attributeは、おそらくあなたcounter: intとセミコロン ( ;) です。パラメータ注釈である AtScript の別の概念を混乱させています。これらは次のように書かれています。

@Template('<div>xxx</div>')
class SomeEl{
    constructor(counter: int){}
}

これは次のように変換されます。これは、あなたが望んでいることだと思います。

  var SomeEl = function SomeEl(counter) {};
  ($traceurRuntime.createClass)(SomeEl, {}, {});
  Object.defineProperty(SomeEl, "annotations", {get: function() {
      return [new Template('<div>xxx</div>')];
    }});
  Object.defineProperty(SomeEl, "parameters", {get: function() {
      return [[int]];
    }});
  return {};
于 2015-02-02T13:56:22.410 に答える