最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Typecasting in Javascriptgoogle closure library - Stack Overflow

programmeradmin2浏览0评论

I am passing a plex object consisting of goog.structs.Set from my content script to background page through chrome.extension.SendMessage API. On the other side, this goog.structs.Set is received as an Object.
How can I typecast it back to goog.structs.Set so that I can call various methods on it?

I am passing a plex object consisting of goog.structs.Set from my content script to background page through chrome.extension.SendMessage API. On the other side, this goog.structs.Set is received as an Object.
How can I typecast it back to goog.structs.Set so that I can call various methods on it?

Share Improve this question edited Aug 30, 2013 at 8:02 RoryKoehein 3,1131 gold badge15 silver badges13 bronze badges asked Aug 29, 2013 at 12:05 vivek.mvivek.m 3,3415 gold badges35 silver badges48 bronze badges 1
  • Before sending the plex object, I convert goog.structs.Set to an array. See @John answer's below for an alternative – vivek.m Commented Sep 9, 2013 at 9:13
Add a ment  | 

3 Answers 3

Reset to default 10

Do you mean for the closure piler?

function receiveStructsSet( aSetObject ){

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

See http://developer.chrome./extensions/messaging.html, you can only pass JSON using chrome.extension.SendMessage.

Personally, I use a simple object as a set and avoid 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.

You can also use the annotation before the function declaration

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

评论列表(0)

  1. 暂无评论