简体   繁体   English

跳过功能关键字会引发语法错误

[英]Skipping function keyword throws syntax error

This example doesn't work: 这个例子不起作用:

var App = React.createClass({
  render() { 
    return (
      <div>
    Hello World  </div>
    )
  }
});

I compile jsx into js on the client side and the browser throws the error, 我将jsx编译为客户端的js,浏览器抛出错误,

Uncaught SyntaxError: Unexpected token (

This example works: 此示例有效:

    var App = React.createClass({
      render: function() {    
    return (
      <div>
    Hello World  </div>
    )
   }       })

So, how does react-router tutorial work without function keyword? 那么,无功能关键字的React-router教程如何工作?

So, how does react-router tutorial work without function keyword? 那么,无功能关键字的React-router教程如何工作?

This is a relatively new thing, being added as part of ES6 (ECMAScript 6, the next version of "JavaScript.") You can create methods like that , basically, this code in ES6: 这是一个相对较新的事物,已作为ES6(ECMAScript 6,下一个“ JavaScript”版本)的一部分添加。您可以在ES6中创建如下代码,基本上是这样的方法

var obj = {
    foo() {
    }
};

is equivalent to this code in ES5 and before: 等效于ES5和更低版本中的以下代码:

var obj = {
    foo: function foo() {
    }
};

(Modulo browser bugs around named function expressions, but no one uses IE8 or Safari 5 anymore, right? Right?) (模块化浏览器在命名函数表达式周围存在错误,但现在没人再使用IE8或Safari 5,对吗?)

The new syntax works on recent versions of Firefox and Chrome. 新语法适用于最新版本的Firefox和Chrome。

That said, I suspect it's just a minor error in that tutorial, as they only do it in a couple of places and use the older syntax in most places. 就是说,我怀疑这只是该教程中的一个小错误,因为他们只在几个地方这样做并且在大多数地方都使用较旧的语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM