Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript node.js需要问题_Javascript_Node.js - Fatal编程技术网

Javascript node.js需要问题

Javascript node.js需要问题,javascript,node.js,Javascript,Node.js,我正在关注这一点 //to_s.js (function(){ String.prototype.to_s = function(){ var str = this.toString(); var convert = function(s){ return eval(s); }; while(/#{(\w+)}/.test(str)){ // bad because I use

我正在关注这一点

//to_s.js
(function(){
String.prototype.to_s = function(){
        var str = this.toString();
        var convert = function(s){
            return eval(s);
        };

        while(/#{(\w+)}/.test(str)){

            // bad  because I use eval...

            var matchStr =RegExp.$1;

            var str = str.replace(/#{(\w+)}/,convert(matchStr));

        }
        return str;
    };
})();

module.exports = String.prototype.to_s;


// test/to_s_test.js

require("./../to_s");

var name = 33;

"hello #{name}".to_s();

我运行到_s_test.js,但发生了“名称未定义”的错误。但我不知道为什么会发生这种情况。但是将“var name=33”更改为name=33,它可以工作。。。你知道吗?提前感谢。

它只在没有
var
的情况下工作,因为
eval
发生在另一个上下文中,因此只能使用您的方法访问全局。如果不声明变量,则会自动创建全局变量;但是,在node.js中,模块中声明的变量不是全局变量

这就是为什么,试图使语言符合其他语言的习惯用法是一个坏主意