Javascript 无效的速记属性初始值设定项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42006503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:49:15  来源:igfitidea点击:

Invalid shorthand property initializer

javascriptnode.js

提问by Pallab Ganguly

I wrote the following code in JavaScript for a node project, but I ran into an error while testing a module. I'm not sure what the error means. Here's my code:

我在 JavaScript 中为节点项目编写了以下代码,但在测试模块时遇到错误。我不确定错误意味着什么。这是我的代码:

var http = require('http');
// makes an http request
var makeRequest = function(message) {
 var options = {
  host: 'localhost',
  port = 8080,
  path : '/',
  method: 'POST'
 }
 // make request and execute function on recieveing response
 var request = http.request(options, function(response) {
  response.on('data', function(data) {
    console.log(data);
  });
 });
 request.write(message);
 request.end();
}
module.exports = makeRequest;

When I try to run this module, it throws the following error:

当我尝试运行此模块时,它会引发以下错误:

$ node make_request.js
/home/pallab/Desktop/make_request.js:8
    path = '/',
    ^^^^^^^^^^
SyntaxError: Invalid shorthand property initializer
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

I dont quite get what this means, and what I can do to resolve this.

我不太明白这意味着什么,以及我能做些什么来解决这个问题。

回答by Diego Faria

Because its an object, the way to assign value to its properties is using :.

因为它是一个对象,所以为其属性赋值的方法是使用:.

Change the =to :to fix the error.

更改=:以修复错误。

var options = {
  host: 'localhost',
  port: 8080,
  path: '/',
  method: 'POST'
 }

回答by Nitin Nema

This error usually comes when you try to assign an object with Equal to(=) sign rather than colon (:)

当您尝试使用等于 (=) 符号而不是冒号 (:) 分配对象时,通常会出现此错误

The correct code should be like:-

正确的代码应该是这样的:-

var options = {
  host: 'localhost',
  port: 8080,
  path: '/',
  method: 'POST'
 }

回答by r.jain

In options object you have used "=" sign to assign value to port but we have to use ":" to assign values to properties in object when using object literal to create an object i.e."{}" ,these curly brackets. Even when you use function expression or create an object inside object you have to use ":" sign. for e.g.:

在选项对象中,您使用“=”符号为端口赋值,但当使用对象字面量创建对象时,我们必须使用“:”为对象中的属性赋值,即“{}”,这些大括号。即使使用函数表达式或在对象内部创建对象,也必须使用“:”符号。例如:

    var rishabh = {
        class:"final year",
        roll:123,
        percent: function(marks1, marks2, marks3){
                      total = marks1 + marks2 + marks3;
                      this.percentage = total/3 }
                    };

john.percent(85,89,95);
console.log(rishabh.percentage);

here we have to use commas "," after each property. but you can use another style to create and initialize an object.

这里我们必须在每个属性后使用逗号“,”。但是您可以使用另一种样式来创建和初始化对象。

var john = new Object():
john.father = "raja";  //1st way to assign using dot operator
john["mother"] = "rani";// 2nd way to assign using brackets and key must be string

回答by akshay_sushir

Use :instead of =

使用:代替=

see the example below that gives an error

请参阅下面给出错误的示例

app.post('/mews', (req, res) => {
if (isValidMew(req.body)) {
    // insert into db
    const mew = {
        name = filter.clean(req.body.name.toString()),
        content = filter.clean(req.body.content.toString()),
        created: new Date()
    };

That gives Syntex Error: invalid shorthand proprty initializer.

这给出了语法错误:无效的速记属性初始值设定项。

Then i replace =with :that's solve this error.

然后我替换=:解决此错误。

app.post('/mews', (req, res) => {
if (isValidMew(req.body)) {
    // insert into db
    const mew = {
        name: filter.clean(req.body.name.toString()),
        content: filter.clean(req.body.content.toString()),
        created: new Date()
    };