通过getProperty / setProperty约定使用ES6 getter和setter的参数是什么? [英] What is the argument for using ES6 getters and setters over getProperty/setProperty convention?

查看:104
本文介绍了通过getProperty / setProperty约定使用ES6 getter和setter的参数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Foo {
getName =()=> this.name;

setName =(name)=> this.name = name;
}

  class Foo {
get name(){
return this.name;
}

set name(name){
this.name = name;
}
}

我可以想到几个例子,其中 ES6吸烟者处于劣势,例如



您不能编写一个基于参数值返回值的getter:

  / ** 
*返回与元素相关联的卡的实例。
*
* @param {HTMLElement}元素
* @return {Card | undefined}
* /
getCard =(element)=> {
return _.find(this.index,{
element:element
});
};但是,如果你使用这个和ES6,你会引入代码风格不一致。

p>

您无法区分直接属性访问和方法访问。

 类Foo {
get name(){
return this.name;
}

get expensive(){
//需要大量计算能力的操作。
}

get randomValue(){
//返回随机值。
}
}

让foo = Foo();

foo.name;
foo.expensive;
foo.randomValue;

缺点是,您正在访问的属性可能需要较大的计算能力并不直观因此应该被记住),或者每次访问它时都会发生变化。



最后,getters / setters不适用于箭头功能。无效示例:

  class Foo {
get name =()=> {
return this.name;
}

set name =(name)=> {
this.name = name;
}
}

将它们暴露给上下文问题。 >

与传统的 get {PropertyName} 相比,使用ES6 getter和setters 的优势是什么? set {PropertyName} 抽象?

解决方案


你不能区分直接的属性访问和方法访问。


这是他们赞成的主要参数。 b
$ b

编写经典Java风格的OO代码的最奇怪的一个难点是,任何具有暴露属性的对象都必须写入getter和setter,并且大量的样板滚动特别是对于大型数据结构类型对象(例如, DTOs )。



所有这些的推理是,你不能只是使属性公开,因为否则你不能在不破坏API的情况下添加逻辑(例如逻辑仅允许设置某些值,或者以稍微不同的方式重构和存储属性,同时仍然暴露相同的外部API或类似的)。请参阅 https://softwareengineering.stackexchange .com / questions / 176876 / why-shouldnt-i-be-using-public-variables-in-my-java-class 在这个周围的一些典型的参数。


$ b $我认为你可以舒服地说,近年来这被认为是合乎逻辑的极限,但这并不意味着它是错误的;通过暴露一个公共领域进行直接访问,您确实暴露了您如何存储数据的内部实现,这意味着您无法轻松或安全地将其更改。



ES6 getters / setters修复此问题。事物可以作为对象上的直接属性可读,不再告诉您有关该属性的实现的任何内容。它本来可以是一个字段,但最近变成了ES6属性访问器,而不改变API。该属性的实现从您的代码库的其余部分隐藏起来,因此更容易更改。


缺点是它不是直观地说,您正在访问的属性可能需要较大的计算能力(因此应记录下来),或者每次访问它时都会更改。




你是对的,这是一个风险。这也是一个getcha()虽然;有一个强大的约定表明,像getName()这样的简单方法不应该在幕后做昂贵的事情,即使它们是方法,如果你这样做,那么你几乎肯定会最终抓住人(包括自己,从现在起6个月)



移动到属性不会改变这个,但是你是对的ES6意味着你不再保证是安全的简单属性访问。答案是真的只是你必须确保你(和其他人)坚持约定和至少原则令人惊讶的是:与现有的简单的吸烟者和戒烟者一样,ES6房产访问者应该做简单的便宜东西,不要在其他地方有奇怪的副作用。


class Foo {
    getName = () => this.name;

    setName = (name) => this.name = name;
}

and

class Foo {
    get name () {
        return this.name;
    }

    set name (name) {
        this.name = name;
    }
}

I can think of several examples where ES6 getters are at a disadvantage, e.g.

You cannot write a getter that will return a value based on a parameter value:

/**
 * Returns an instance of Card associated with an element.
 *
 * @param {HTMLElement} element
 * @return {Card|undefined}
 */
getCard = (element) => {
    return _.find(this.index, {
        element: element
    });
};

That's okay, however, if you use this and ES6, you are introducing code style inconsistency.

You cannot distinguish between a direct property access and method access.

class Foo {
    get name () {
        return this.name;
    }

    get expensive () {
        // An operation that takes a lot of computational power.
    }

    get randomValue () {
        // Returns random value.
    }
}

let foo = Foo();

foo.name;
foo.expensive;
foo.randomValue;

The disadvantage is that it is not intuitive that a property that you are accessing might require heavy computational power (and therefore should be memoized) or that it changes every time you access it.

Finally, getters/setters do not work with arrow functions. Invalid example:

class Foo {
    get name = () => {
        return this.name;
    }

    set name = (name) => {
        this.name = name;
    }
}

Which exposes them to the context issues.

What is the advantage of using ES6 getters and setters over the conventional get{PropertyName} and set{PropertyName} abstraction?

解决方案

You cannot distinguish between a direct property access and method access.

This is the main argument in their favour.

One of the weirdest great pains of writing classic Java-style OO code is that any object with an exposed property on it has to have getters and setters written, and a huge amount of boilerplate comes rolling out of this, especially for large data-structure type objects (e.g. DTOs).

The reasoning for all of these is that you can't just make properties public, because otherwise you can't ever add logic to them without breaking the API (e.g. logic to only allow settings certain values, or to refactor and store a property in a slightly different way while still exposing the same external API, or similar). See https://softwareengineering.stackexchange.com/questions/176876/why-shouldnt-i-be-using-public-variables-in-my-java-class for some typical arguments around this.

I think you could comfortably say that this has been taken to its logical extreme in recent years, but that doesn't mean it's wrong; by exposing a public field for direct access you are indeed exposing the internal implementation of how you're storing that data, and that means you can't change it as easily or safely any more.

ES6 getters/setters fix this. The fact that something is readable as a direct property on an object no longer tells you anything about the implementation of that property. It could have been a field originally, but recently turned into a ES6 property accessor instead, without the API changing. The implementation of the property is hidden from the rest of your codebase, and is therefore easier to change.

The disadvantage is that it is not intuitive that a property that you are accessing might require heavy computational power (and therefore should be memoized) or that it changes every time you access it.

You're right, this is a risk. That's also a gotcha with any getX() though; there's a strong convention suggesting that nice simple methods like 'getName()' shouldn't ever be doing expensive things behind the scenes, even if they are methods, and if you break that you'll almost certainly end up catching people out (including yourself, 6 months from now)

Moving to properties doesn't change this, but you're right that ES6 means you're no longer guaranteed to be safe for simple property accesses. The answer is really just that you have to make sure you (and everybody else) sticks with convention and Principle of Least Astonishment: as with existing simple looking getters and setters, ES6 property accessors should do simple cheap things, and shouldn't have strange side effects elsewhere.

这篇关于通过getProperty / setProperty约定使用ES6 getter和setter的参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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