requirejs and ByteBuffer

醉酒当歌 提交于 2019-12-11 10:06:17

问题


im an javascript newbie and google didnt helps:

I tryed to load ByteBuffer.js in an require.js module:

define(['js/ByteBufferAB'], function (ByteBufferAB) {

var MessageBase = function () {
    this._version = 0; // unsinged int 16 bits
    this._dataType = "";

};

MessageBase.prototype.toBytes = function () {
    //console.log( new ByteBufferAB(58));
    var headerBytes = new ByteBufferAB(58); // <-- here comes the error
    headerBytes.clear();
    return headerBytes;
};


return MessageBase;

});

with the same syntax math.js is properly loaded.

But with ByteBufferAB.js the following error comes:

Uncaught TypeError: undefined is not a function

What am I doing wrong? Thank you for your help


回答1:


In your define call you refer to the module as js/ByteBufferAB so RequireJS looks for a module named js/ByteBufferAB. However, the module defines itself as ByteBuffer:

/* AMD */ else if (typeof define === 'function' && define["amd"])
    define("ByteBuffer", ["Long"], function(Long) { return  loadByteBuffer(Long); });

Because the module name is hardcoded, you need to have a mapping like this in your paths in the configuration you give to RequireJS:

ByteBuffer: "js/ByteBufferAB"

and you need to refer to the module as ByteBuffer in your define call.

None of this would be required if the developers for this library had not hardcoded a name but they have, and so here we are.



来源:https://stackoverflow.com/questions/30644763/requirejs-and-bytebuffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!