简体   繁体   English

带有箭头功能的 ES6 getter/setter

[英]ES6 getter/setter with arrow function

I'm using babel6 and for my pet project I'm creating a wrapper for XMLHttpRequest , for the methods I can use:我正在使用babel6 ,对于我的宠物项目,我正在为XMLHttpRequest创建一个包装器,用于我可以使用的方法:

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

But for the properties arrow function doesn't work.但是对于属性箭头功能不起作用。 This works:这有效:

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

But I can't use the arrow function:但我不能使用箭头功能:

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

Is this intentional?这是故意的吗?

According to the ES2015 grammar, a property on an object literal can only be one of three things:根据 ES2015 语法, 对象字面量上的属性只能是以下三种情况之一:

PropertyDefinition :属性定义

  • IdentifierReference标识符参考
  • PropertyName : AssignmentExpression属性:赋值表达式
  • MethodDefinition方法定义

The only one of these type that allows a leading get is MethodDefinition :这些类型中唯一允许前导get的是MethodDefinition

MethodDefinition :方法定义

  • PropertyName ( StrictFormalParameters ) { FunctionBody } PropertyName ( StrictFormalParameters ) { FunctionBody }
  • GeneratorMethod生成器方法
  • get PropertyName ( ) { FunctionBody } get PropertyName ( ) { FunctionBody }
  • set PropertyName ( PropertySetParameterList ) { FunctionBody } set PropertyName ( PropertySetParameterList ) { FunctionBody }

As you can see, the get form follows a very limited grammar that must be of the form如您所见, get形式遵循非常有限的语法,必须是以下形式

get NAME () { BODY }

The grammar does not allow functions of the form get NAME = ... .语法不允许get NAME = ...形式的函数。

The accepted answer is great.接受的答案很棒。 It's the best if you're willing to use normal function syntax instead of compact "arrow function syntax".如果您愿意使用普通函数语法而不是紧凑的“箭头函数语法”,那是最好的。

But maybe you really like arrow functions;但也许你真的很喜欢箭头函数; maybe you use the arrow function for another reason which a normal function syntax cannot replace ;也许您出于其他原因使用箭头函数,这是普通函数语法无法替代的 you may need a different solution.您可能需要不同的解决方案。

For example, I notice OP uses this , you may want to bind this lexically;例如,我注意到 OP 使用this ,你可能想在词法上绑定this aka "non-binding of this" ), and arrow functions are good for that lexical binding. aka “non-binding of this” ),箭头函数对于词法绑定很有用。

You can still use an arrow function with a getter via the Object.defineProperty technique.您仍然可以通过Object.defineProperty技术将箭头函数与 getter 一起使用。

{
  ...
  Object.defineProperty(your_obj, 'status', { 
     get : () => this.xhr.status 
  });
  ...
}

See mentions of object initialization technique (aka get NAME() {...} ) vs the defineProperty technique (aka get : ()=>{} ) .请参阅object initialization技术(又名get NAME() {...}defineProperty技术(又名get : ()=>{}的提及。 There is at least one significant difference, using defineProperty requires the variables already exists:至少有一个显着差异,使用defineProperty需要变量已经存在:

Defining a getter on existing objects现有对象上定义 getter

ie with Object.defineProperty you must ensure that your_obj (in my example) exists and is saved into a variable (whereas with a object-initialization you could return an object-literal in your object initialization: {..., get(){ }, ... } ).即使用Object.defineProperty您必须确保your_obj (在我的示例中)存在并保存到变量中(而使用object-initialization ,您可以在对象初始化中返回对象文字: {..., get(){ }, ... } )。 More info on Object.defineProperty specifically, here有关Object.defineProperty的更多信息,请点击此处

Object.defineProperty(...) seems to have comparable browser support to the get NAME(){...} syntax; Object.defineProperty(...)似乎具有与get NAME(){...}语法相当的浏览器支持; modern browsers, IE 9.现代浏览器,IE 9。

        _getvalue: () => {
            return this.array.length;
        }
        get value(): number {
            return this._getvalue();;
        }

access to parent object in class 访问类中的父对象

it is worked for me :P 它为我工作:P

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

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