【问题标题】:What is var name = (param) => {} in JS [duplicate]JS中的var name =(param)=> {}是什么[重复]
【发布时间】:2015-05-11 21:12:47
【问题描述】:

我正在处理一个 React-Router 测试存根示例,我以前从未见过这种类约定。我猜这是 ES6 特有的?

var stubRouterContext = (Component, props, stubs) => {
    function RouterStub() { }

    Object.assign(RouterStub, {
        makePath () {},
        makeHref () {},
        transitionTo () {},
        replaceWith () {},
        goBack () {},
        getCurrentPath () {},
        getCurrentRoutes () {},
        getCurrentPathname () {},
        getCurrentParams () {},
        getCurrentQuery () {},
        isActive () {},
        getRouteAtDepth() {},
        setRouteComponentAtDepth() {}
    }, stubs);

    return React.createClass({
        childContextTypes: {
            router: React.PropTypes.func,
            routeDepth: React.PropTypes.number
        },

        getChildContext () {
            return {
                router: RouterStub,
                routeDepth: 0
            };
        },

        render () {
            return <Component {...props} />
        }
    });
};

为什么我会得到 TypeError: object is not function when I do this?

var Subject = stubRouterContext(Handler, props);

这里是文档https://github.com/rackt/react-router/blob/master/docs/guides/testing.md的链接

【问题讨论】:

  • 是的,是ES6箭头函数语法,看这里github.com/lukehoban/es6features
  • 你在编译你的代码吗?怎么样?
  • 没关系,这是重复的。 @JMM,我正在使用 babel,但我只是忘记导出模块。关闭。谢谢。

标签: javascript reactjs react-router


【解决方案1】:

它是 ES6 特定的,它是箭头的简写:https://github.com/lukehoban/es6features#arrows

“箭头是使用=&gt; 语法的函数简写。它们在语法上类似于C#、Java 8 和CoffeeScript 中的相关功能。”

【讨论】:

    猜你喜欢
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    相关资源
    最近更新 更多