简体   繁体   English

在Javascript / google关闭库中进行类型转换

[英]Typecasting in Javascript / google closure library

I am passing a complex object consisting of goog.structs.Set from my content script to background page through chrome.extension.SendMessage API. 我正在通过chrome.extension.SendMessage API将包含goog.structs.Set的复杂对象从我的内容脚本传递到后台页面。 On the other side, this goog.structs.Set is received as an Object . 另一方面,这个goog.structs.Set作为Object接收。
How can I typecast it back to goog.structs.Set so that I can call various methods on it? 我怎样才能将它强制转换回goog.structs.Set以便我可以调用它上面的各种方法?

Do you mean for the closure compiler? 你的意思是闭包编译器吗?

function receiveStructsSet( aSetObject ){

    var mySet =  /** @type {goog.structs.Set} */ (aSetObject); 
}

See http://developer.chrome.com/extensions/messaging.html , you can only pass JSON using chrome.extension.SendMessage. 请参阅http://developer.chrome.com/extensions/messaging.html ,您只能使用chrome.extension.SendMessage传递JSON。

Personally, I use a simple object as a set and avoid goog.structs.Set: 就个人而言,我使用一个简单的对象作为集合,并避免goog.structs.Set:

var MySet = Object.create(null);

If use must use goog.structs.Set, you will need to serialize and deserialize it to JSON. 如果use必须使用goog.structs.Set,则需要将其序列化并反序列化为JSON。

You can also use the annotation before the function declaration 您还可以在函数声明之前使用注释

/**
 *  @param {goog.structs.Set} aSetObject description of object
 */
function receiveStructsSet( aSetObject ){
  aSetObject.getCount();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM