首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用get和set是可以理解的?

使用get和set是可以理解的?
EN

Stack Overflow用户
提问于 2017-07-23 18:05:36
回答 1查看 628关注 0票数 1

我知道ES 6而且我有疑问-使用get和set是可以理解的?

例如:

代码语言:javascript
复制
class Country {
    get cities() {
        return this.citiesList;
    }
    set cities(value) {
        this.citiesList = value;
    }
}

let country = new Country();
country.cities = ['Tokyo', 'London'];
console.log(country.cities);

为什么建议使用set并得到这样的结果:

代码语言:javascript
复制
class Country {
    getCities() {
        return this.cities;
    }
    setCities(value) {
        this.cities = value;
    }
}

let country = new Country();
country.setCities(['Tokyo', 'London']);
console.log(country.getCities());

在第二个例子中,我可以:

  1. this.cities而不是this.citiesList (get/set城市与this.cities重复)
  2. 当使用类时,它看起来更好: country.getCities()而不是country.cities。

我现在有两个问题:

哪种方式更好:

( A)用方法设置城市(cities.setCities()或城市= [])

( B)将城市设为构造函数:

代码语言:javascript
复制
class Country {
    constructor (cities) 
    {
        this.cities = cities;
    }
}

最后一个问题:

如果我想要列出城市的数量并使用构造函数,那么.

a)

代码语言:javascript
复制
class Country {
    constructor (cities) 
    {
        this.cities = cities;
        this.citiesLength = cities.length;
    }
    test () 
    {
        //operations...
        let op = this.citiesLength * 10;
    }
}

b)

代码语言:javascript
复制
class Country {
    constructor (cities) 
    {
        this.cities = cities;
    }
    citiesLength() 
    {
        return cities.length;
    }
    test () 
    {
        //operations...
        let op = this.citiesLength()  * 10;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-23 18:15:48

会在字里行间回答。

代码语言:javascript
复制
class Country {
    get cities() {
        return this.citiesList;
    }
    set cities(value) {
        this.citiesList = value;
    }
}

let country = new Country();
country.cities = ['Tokyo', 'London'];
console.log(country.cities);

为什么建议使用set并得到这样的结果:

代码语言:javascript
复制
class Country {
    getCities() {
        return this.cities;
    }
    setCities(value) {
        this.cities = value;
    }
}

let country = new Country();
country.setCities(['Tokyo', 'London']);
console.log(country.getCities());

R)这是语法糖,两者都修改对象变量(记住JS类也是对象)。我更喜欢用户getter和setter,因为代码更少,更容易读懂。

在第二个例子中,我可以:

当使用类时,this.cities而不是this.citiesList (get/set cities与this.cities重复),它看起来更好: country.getCities()而不是country.cities。我现在有两个问题:

哪种方式更好:

( A)用方法设置城市(cities.setCities()或城市= [])

( B)将城市设为构造函数:

代码语言:javascript
复制
class Country {
    constructor (cities) 
    {
        this.cities = cities;
    }
}

( R)它取决于,当您知道总是要设置这些属性时,请使用构造函数,并且从该类创建的每个对象都有城市。

最后一个问题:

如果我想要列出城市的数量并使用构造函数,那么.

a)

代码语言:javascript
复制
class Country {
    constructor (cities) 
    {
        this.cities = cities;
        this.citiesLength = cities.length;
    }
    test () 
    {
        //operations...
        let op = this.citiesLength * 10;
    }
}

b)

代码语言:javascript
复制
class Country {
    constructor (cities) 
    {
        this.cities = cities;
    }
    citiesLength() 
    {
        return cities.length;
    }
    test () 
    {
        //operations...
        let op = this.citiesLength()  * 10;
    }
}

**R) B,因为在A中,每次从数组中添加或删除城市时,都需要设置citiesLenght。**

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45268317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档