nodejs的Buffer有办法像c的结构体那样用吗

如题所述

第1个回答  2016-12-10
可以使用ref和ref-struct模块
比如想要定义下面这样的结构

struct timeval {
time_t tv_sec; /* seconds since Jan. 1, 1970 */
suseconds_t tv_usec; /* and microseconds */
};
如果在nodejs里面用ref和ref-struct模块来定义,就按下面这种写法
var ref = require('ref');
var StructType = require('ref-struct');
// define the time types
var time_t = ref.types.long;
var suseconds_t = ref.types.long;
// define the "timeval" struct type
var timeval = StructType({
tv_sec: time_t,
tv_usec: suseconds_t
});
// now we can create instances of it
var tv = new timeval;
上面这些一般是配合ffi模块一起用的,
详情参考:
https://github.com/TooTallNate/ref-struct#readme
https://github.com/TooTallNate/ref#readme
http://github.com/node-ffi/node-ffi
第2个回答  2016-12-10
Buffer是流,没有key/value结构,JS的key/value数据结构是object。
var u = {
name: "党志明",
message: "..."
}
u.name
u.name = ..本回答被网友采纳