小编Joã*_*imo的帖子

为什么{key:"value"}与{"key":"value"}不同?

直接来自节点REPL:

> d1 = {key : "value"}
{ key: 'value' }
> d2 = {"key" : "value"}
{ key: 'value' }
> d1 == d2
false
Run Code Online (Sandbox Code Playgroud)

为什么d1与d2不同?

javascript object

1
推荐指数
1
解决办法
125
查看次数

为什么JSON.parse('{"key":"value"}')做得很好,但JSON.parse('{key:"value"}')不行?

从节点REPL:

> JSON.parse('{"key" : "value"}')
{ key: 'value' }
> JSON.parse('{key : "value"}')
SyntaxError: Unexpected token ILLEGAL
    at Object.parse (native)
    at [object Context]:1:6
    at Interface.<anonymous> (repl.js:171:22)
        at Interface.emit (events.js:64:17)
    at Interface._onLine (readline.js:153:10)
    at Interface._line (readline.js:408:8)
    at Interface._ttyWrite (readline.js:585:14)
    at ReadStream.<anonymous> (readline.js:73:12)
    at ReadStream.emit (events.js:81:20)
    at ReadStream._emitKey (tty_posix.js:307:10)
Run Code Online (Sandbox Code Playgroud)

javascript json node.js

1
推荐指数
1
解决办法
2963
查看次数

正则表达式匹配javascript函数的定义并确保它返回一些东西?

我需要确保一些提交的代码具有一个名为mapfndefined 的函数,并且该函数返回一个结果.我提出了以下正则表达式: mapfn\s+?\=\s+?function\s+?\(split\)\s+?\{.+?return\(result\).+?\}匹配类似的东西

mapfn = function (split) {
    var i = 5+4;
    for (var j = 0; j < 10; j++) {
        i += j*Math.random()*10;
    }
    var result = i;
    return(result)
}
Run Code Online (Sandbox Code Playgroud)

这是可取的但是如果我使用这个代码来获得一个示例闭包编译器并获得类似的东西mapfn=function(){for(var b=9,a=0;a<10;a++)b+=a*Math.random()*10;return b};,那么正则表达式是无用的.此外,用户提交类似的东西

function mapfn (split) {
    var i = 5+4;
    for (var j = 0; j < 10; j++) {
        i += j*Math.random()*10;
    }
    var result = i;
    return(result)
}
Run Code Online (Sandbox Code Playgroud)

然后正则表达式也没用.

对于这个问题,我觉得有一个更优雅的解决方案,而不是为这个工作提供5或6个正则表达式并尝试匹配其中任何一个.

javascript regex function

1
推荐指数
1
解决办法
1327
查看次数

在为Node.JS编写的非常低级别的TCP Server上进行身份验证?

如何在为Node.JS编写的TCP服务器中实现类似于HTTP Basic身份验证的内容?基本TCP服务器的代码如下:

// Load the net module to create a tcp server.
var net = require('net');

// Setup a tcp server
var server = net.createServer(function (socket) {

  // Every time someone connects, tell them hello and then close the connection.
  socket.addListener("connect", function () {
    console.log("Connection from " + socket.remoteAddress);
    socket.end("Hello World\n");
  });

});

// Fire up the server bound to port 7000 on localhost
server.listen(7000, "localhost");

// Put a friendly message on the terminal
console.log("TCP server listening on port 7000 …
Run Code Online (Sandbox Code Playgroud)

authentication tcp tcpclient node.js

0
推荐指数
1
解决办法
2185
查看次数

标签 统计

javascript ×3

node.js ×2

authentication ×1

function ×1

json ×1

object ×1

regex ×1

tcp ×1

tcpclient ×1