简体   繁体   English

将JavaScript代码迁移到具有String.prototype扩展名的node.js模块

[英]Migrating JavaScript code to node.js modules with String.prototype extension

I'm trying to migrate Crockfords TDOP based parser to Node.js, as I want to use it as a base for my own parser. 我试图将基于Crockfords TDOP的解析器迁移到Node.js,因为我想将其用作自己的解析器的基础。

In his code he extends the String object with a new method called "tokens": 在他的代码中,他使用名为“ tokens”的新方法扩展了String对象:

String.prototype.tokens = function (prefix, suffix) {
[...]

Source: https://github.com/douglascrockford/TDOP/blob/master/tokens.js#L25 来源: https : //github.com/douglascrockford/TDOP/blob/master/tokens.js#L25

This method gets called by his parser later: 此方法稍后将由他的解析器调用:

[...]

return function (source) {
    tokens = source.tokens('=<>!+-*&|/%^', '=<>&|');
    token_nr = 0;
    new_scope();
    advance();
    var s = statements();
    advance("(end)");
    scope.pop();
    return s;
};

[...]

Source: https://github.com/douglascrockford/TDOP/blob/master/parse.js#L520 来源: https//github.com/douglascrockford/TDOP/blob/master/parse.js#L520

In the browser version he simply loads all those files which seem to be added to the global scope so all works fine: 在浏览器版本中,他只是加载了所有似乎已添加到全局范围的文件,因此一切正常:

https://github.com/douglascrockford/TDOP/blob/master/index.html#L58 https://github.com/douglascrockford/TDOP/blob/master/index.html#L58

I'm now trying to get this to work in Node. 我现在正在尝试使其在Node中工作。 For that I started stripping out the stuff which is browser based and read a sample file from filesystem. 为此,我开始剥离基于浏览器的内容,并从文件系统中读取示例文件。

It seems to do something up to the point where the String.tokens method gets called, in there I get 'my string' has no method 'tokens' . 似乎可以做到String.tokens方法被调用的地方,在那里我得到的'my string' has no method 'tokens' In my limited understanding of scopes I suspect that this is because node seems to create its own "global" scope for each module. 在对范围的有限理解中,我怀疑这是因为节点似乎为每个模块创建了自己的“全局”范围。 My big question now is how can I extend the String object in a way that this will work out? 我现在的大问题是如何以一种可行的方式扩展String对象? I tried defining this method in many different scopes in my sample code but I couldn't get a single one of them to work. 我尝试在示例代码中的许多不同范围内定义此方法,但无法单独使用它们。 My idea is to have the parse method as a module and then assign String.prototype.tokens == mymodule.tokens from that file. 我的想法是将parse方法作为模块,然后从该文件分配String.prototype.tokens == mymodule.tokens

Is this the right approach and if so, where the heck do I have to define the String.prototype.tokens method that my code will be able to find it? 这是正确的方法吗?如果是这样,我必须在哪里定义我的代码能够找到的String.prototype.tokens方法?

I've created a github repository with my node code, pull requests are welcome ;) 我已经用节点代码创建了一个github存储库,欢迎拉取请求;)

https://github.com/ktk/js-scope-test/blob/master/node.js https://github.com/ktk/js-scope-test/blob/master/node.js

This did work all the time as Bubbles pointed out. 正如Bubbles指出的那样,这一直有效。 I forgot to call toString() on the Buffer object I got from fs.readFile... 我忘了在从fs.readFile获得的Buffer对象上调用toString()...

Sorry & thanks for the hints. 对不起,谢谢您的提示。

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

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