Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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 简单的web服务器示例-为什么必须将属性名称加引号?_Javascript_Node.js - Fatal编程技术网

Javascript 简单的web服务器示例-为什么必须将属性名称加引号?

Javascript 简单的web服务器示例-为什么必须将属性名称加引号?,javascript,node.js,Javascript,Node.js,考虑以下示例: var http = require('http'); var server = http.createServer(function(request, response) { response.writeHead({ 'content-type': 'text/plain' }); response.end('Hello world!'); }); server.listen(8000); 为什么我必须将内容类型属性名称置于引号中?writeHead不需

考虑以下示例:

var http = require('http');
var server = http.createServer(function(request, response) {
  response.writeHead({
    'content-type': 'text/plain'
  });
  response.end('Hello world!');
});

server.listen(8000);
为什么我必须将
内容类型
属性名称置于引号中?writeHead不需要一个普通的JS对象吗?为什么我不能写这样的东西:

{
  content-type: 'text/plain'
}

如果JavaScript对象文本的属性名不是a(即可以用作变量名的名称),则必须引用该名称;整数显然也可以。由于破折号字符(
-
)不是标识符的有效部分,因此必须引用字符串

varo;
o={content type:'text/plain'};//=>SyntaxError:意外标记“-”
o={'content-type':'text/plain'};//=>好啊
o={contentType:'text/plain'};//=>好啊
o={123:456};//=>好啊
o={$x:123};//=>好啊
o={π:234};//=>好啊