【问题标题】:JavaScript simple code not runningJavaScript 简单代码未运行
【发布时间】:2017-11-04 08:58:18
【问题描述】:

我正在看教程,我尝试运行以下代码,但没有运行。

var person = {
  'address': {
    'street': 'Rose Road',
    'city': 'somewhere',
    'state': 'CA'
  },
  'isfromState': fucntion(state) {
    return (this.address.state === state);
  }
}
console.log(person.isfromState('blah'));

相反,它给了我这个错误Exception: SyntaxError: missing } after property list,我找不到错误。

【问题讨论】:

  • fucntion 应该是function

标签: javascript compiler-errors syntax-error


【解决方案1】:
'isfromState': fucntion(state) { // <--- fucntion isn't a keyword
  return (this.address.state === state);
}

应该是:

'isfromState': function(state) { // <--- use function
  return (this.address.state === state);
}

【讨论】:

  • 鉴于错误这不是问题(虽然肯定是 a 问题!)。虽然我尝试了一些变体,但无法报告 SyntaxError,但它经常在某处缺少逗号,而 sn-p 则不是这种情况。
猜你喜欢
  • 2017-12-19
  • 1970-01-01
  • 2011-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 2019-11-17
相关资源
最近更新 更多