Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 NodeJS中奇怪的数组行为_Javascript_Arrays_Node.js - Fatal编程技术网

Javascript NodeJS中奇怪的数组行为

Javascript NodeJS中奇怪的数组行为,javascript,arrays,node.js,Javascript,Arrays,Node.js,我为NodeJS编写了以下代码: /* server.js */ 'use strict'; const http = require('http'), url = require('url'); METHODS = ['GET','POST','PUT','DELETE'], _routePathIndex = Array.apply(null, Array(METHODS.length)).map(() => {return []}), _routeM

我为NodeJS编写了以下代码:

/* server.js */
'use strict';

const http = require('http'),
    url = require('url');
    METHODS = ['GET','POST','PUT','DELETE'],
    _routePathIndex = Array.apply(null, Array(METHODS.length)).map(() => {return []}),
    _routeMethodIndex = _routePathIndex.slice(),
    _server = http.createServer();

_server.on('request', (req, res) => {
    let parsed = url.parse(req.url),
        methodIndexVal = METHODS.indexOf(req.method),
        PathIndexVal = _routePathIndex[methodIndexVal].indexOf(parsed.pathname);

    _routeMethodIndex[methodIndexVal][PathIndexVal](req, res);
});

module.exports = _init();

function _init(){
    let rs = { listen: _listen };
    METHODS.forEach( (val,i) => {
        rs[val.toLowerCase()] = function(route, handler){
            _routePathIndex[i].push(route);
            _routeMethodIndex[i].push(handler);
        };
    });

    return rs;
};

function _listen(port, callback){
    _server.listen(port, callback);
}
为了测试这一点,我有一个非常简单的脚本:

/* server.test.js */
var app = require('./server.js');
app.get('/', (req,res) => { console.log(req, res); });
app.listen(3000, () => { console.log('listening at port', 3000) });
奇怪之处始于server.test.js的第2行,它在server.js中执行以下代码块,我添加了注释以显示
\u routePathIndex
\u routeMethodIndex
的值

...
        rs[val.toLowerCase()] = function(route, handler){
            /* _routePathIndex:    [ [], [], [], [], ]
               _routeMethodIndex:  [ [], [], [], [], ] */
            _routePathIndex[i].push(route);

            /* _routePathIndex:    [ ['/'], [], [], [], ]
               _routeMethodIndex:  [ ['/'], [], [], [], ] */
            _routeMethodIndex[i].push(handler);


            /* _routePathIndex:    [ ['/', [Function]], [], [], [], ]
               _routeMethodIndex:  [ ['/', [Function]], [], [], [], ] */
        }; 
...

我的问题是,为什么数组充当相互引用的对象?

起初,我认为可能是
.slice()
在引用,但我通过在相同的环境中运行以下脚本来证明这一点:

var a = [], b = a.slice();
a.push(1);
console.log(a,b); // [1] [0]
另一件事是,当我不执行
.slice()
技巧并将代码重构成这样时

...
    _routePathIndex = Array.apply(null, Array(METHODS.length)).map(() => {return []}),
    _routeMethodIndex = Array.apply(null, Array(METHODS.length)).map(() => {return []}),
奇怪的引用行为消失了,代码工作得很完美

有关添加的信息,我正在使用
节点-v
v5.4.1


我还尝试使用
[]克隆数组。concat(\u routePathIndex)
但它仍然有一种奇怪的行为

切片
只做一个浅拷贝,即
\u routePathIndex
\u routeMethodIndex
是不同的,但它们的元素是相同的。考虑这个简化的例子:

a=[],[];
b=a.切片();
b[0]。推送(1);

document.write(a)
Array.apply(null,Array(METHODS.length)).map(()=>{return[]})
似乎(对我来说)是一种复杂且不可读的编写
Array(METHODS.length)的方式。fill([])
不能很快接受答案,所以规则,不管怎样,你用什么来制作这张很酷的图片?@jkris:-前开发了这个供我自己使用。