首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何访问traceur atscript中的字段注释?

如何访问traceur atscript中的字段注释?
EN

Stack Overflow用户
提问于 2015-02-01 16:26:34
回答 1查看 236关注 0票数 0
代码语言:javascript
复制
// 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中的字段注释?我只能访问类注释,但不能在类中使用字段注释,希望您能提供帮助。

上面的代码被转换成

代码语言:javascript
复制
$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);
})();

我看不出被认为是归档countercounter注释

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-02 13:56:22

这里有几处出错,构造函数上的注释与类上的注释相同。

代码语言:javascript
复制
@Template('<div>xxx</div>')
class SomeEl{
   @Attribute
   constructor(){}
}

上述内容转化为:

代码语言:javascript
复制
Object.defineProperty(SomeEl, "annotations", {get: function() {
  return [new Template('<div>xxx</div>'), new Attribute];
}});

注意,构造函数上的注释与类上的注释相同。任何其他函数上的注释都会将注释放在该函数上,但构造函数和类本质上是相同的。

new Attribute没有出现在注释中的原因很可能是counter: int和分号(;)。您混淆了AtScript的另一个概念,即参数注释。这些是这样写的:

代码语言:javascript
复制
@Template('<div>xxx</div>')
class SomeEl{
    constructor(counter: int){}
}

这转化为以下几点,这就是我相信你想要的:

代码语言:javascript
复制
  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 {};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28264922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档