带箭头功能的ES6吸气剂/吸气剂 [英] ES6 getter/setter with arrow function

查看:93
本文介绍了带箭头功能的ES6吸气剂/吸气剂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用babel6,对于我的宠物项目,我正在为XMLHttpRequest创建包装器,以使用以下方法:

I'm using babel6 and for my pet project I'm creating a wrapper for XMLHttpRequest, for the methods I can use:

open = (method, url, something) => {
  return this.xhr.open(method, url, something);
}

但是对于属性箭头功能不起作用

but for the properties arrow function doesn't work

这有效:

get status() { return this.xhr.status; }

但我不能使用

get status = () => this.xhr.status;

这是故意的吗?

推荐答案

根据ES2015语法,

According to the ES2015 grammar, a property on an object literal can only be one of four things:

属性定义:

  • IdentifierReference
  • 属性名称 : AssignmentExpression
  • MethodDefinition
  • IdentifierReference
  • PropertyName : AssignmentExpression
  • MethodDefinition

这些类型中唯一允许前导get的类型是 MethodDefinition :

The only one of these type that allows a leading get is MethodDefinition:

MethodDefinition :

  • 属性名称 ( StrictFormalParameters ) { FunctionBody }
  • GeneratorMethod
  • get 属性名称 ( ) { FunctionBody }
  • set PropertyName ( PropertySetParameterList ) { FunctionBody }
  • PropertyName ( StrictFormalParameters ) { FunctionBody }
  • GeneratorMethod
  • get PropertyName ( ) { FunctionBody }
  • set PropertyName ( PropertySetParameterList ) { FunctionBody }

如您所见,get形式遵循的语法非常有限,必须具有以下形式

As you can see, the get form follows a very limited grammar that must be of the form

get NAME () { BODY }

语法不允许使用get NAME = ...形式的函数.

The grammar does not allow functions of the form get NAME = ....

这篇关于带箭头功能的ES6吸气剂/吸气剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆