Javascript ES 6-使用get和set可以理解吗?
我学习了ES6,我有疑问-使用get和set是可以理解的吗 例如:Javascript ES 6-使用get和set可以理解吗?,javascript,oop,ecmascript-6,Javascript,Oop,Ecmascript 6,我学习了ES6,我有疑问-使用get和set是可以理解的吗 例如: class Country { get cities() { return this.citiesList; } set cities(value) { this.citiesList = value; } } let country = new Country(); country.cities = ['Tokyo', 'London']; console.lo
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);
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和get,如下所示:
class Country {
getCities() {
return this.cities;
}
setCities(value) {
this.cities = value;
}
}
let country = new Country();
country.setCities(['Tokyo', 'London']);
console.log(country.getCities());
class Country {
getCities() {
return this.cities;
}
setCities(value) {
this.cities = value;
}
}
let country = new Country();
country.setCities(['Tokyo', 'London']);
console.log(country.getCities());
在第二个例子中,我可以:
class Country {
constructor (cities)
{
this.cities = cities;
}
}
class Country {
constructor (cities)
{
this.cities = cities;
}
}
最后一个问题:
如果我想列出城市的数量,并使用构造器,那么
(A)
或
(B)
将在字里行间回答 为什么建议改为使用set和get,如下所示:
class Country {
getCities() {
return this.cities;
}
setCities(value) {
this.cities = value;
}
}
let country = new Country();
country.setCities(['Tokyo', 'London']);
console.log(country.getCities());
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(获取/设置cities会从this.cities复制自身)
使用类时,它看起来比country.cities更好:country.getCities()。
我还有两个问题:
哪种方式更好:
A) 按方法设置城市(cities.setCities()或cities=[])
B) 将城市设置为:
class Country {
constructor (cities)
{
this.cities = cities;
}
}
class Country {
constructor (cities)
{
this.cities = cities;
}
}
R)视情况而定,当您知道总是要设置这些属性,并且从此类创建的每个对象都将具有城市时,请使用构造函数。
最后一个问题:
如果我想列出城市的数量,并使用构造器,那么
(A)
或
(B)
**R) B因为在A中,每次从数组中添加或删除城市时,都需要设置CitiesLength** 将在行间应答 为什么建议改为使用set和get,如下所示:
class Country {
getCities() {
return this.cities;
}
setCities(value) {
this.cities = value;
}
}
let country = new Country();
country.setCities(['Tokyo', 'London']);
console.log(country.getCities());
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(获取/设置cities会从this.cities复制自身)
使用类时,它看起来比country.cities更好:country.getCities()。
我还有两个问题:
哪种方式更好:
A) 按方法设置城市(cities.setCities()或cities=[])
B) 将城市设置为:
class Country {
constructor (cities)
{
this.cities = cities;
}
}
class Country {
constructor (cities)
{
this.cities = cities;
}
}
R)视情况而定,当您知道总是要设置这些属性,并且从此类创建的每个对象都将具有城市时,请使用构造函数。
最后一个问题:
如果我想列出城市的数量,并使用构造器,那么
(A)
或
(B)
**R) B因为在A中,每次从数组中添加或删除城市时,都需要设置CitiesLength** 请不要一次在一个以上的网站上发布问题。您好,我从上面了解到,您的问题是哪种方式更好:这看起来像是一个基于观点的问题,在SO中是离题的。请改写问题“为什么推荐”推荐在哪里?“哪种方式更好”问题中使用的“更好”的定义是什么?请不要一次在超过一个网站上发布问题。您好,我从上面了解到,您的问题是哪种方式更好:这看起来像是一个基于意见的问题,在SO中是离题的。请改写问题“为什么推荐”推荐在哪里?“哪种方式更好”问题中使用的“更好”的定义是什么?“记住在JS中类也是对象”记住,类在JS中实际上并不存在,只有对象和原型。我知道,这就是为什么我说类只是语法糖,类是对象。最后一个答案中选择A的问题是,它只存储长度一次,并且当数组的长度改变时不会改变。无论何时从数组中添加或删除项,甚至将变量重新分配给其他数组,都必须自己更新变量。对于选项B,它本质上使用了
length
属性的getter,该属性将按预期进行更新。我错过了从一个分离的变量中提取lenght的部分。我将修改我的回答。“记住在JS中类也是对象”记住,类在JS中实际上并不存在,只有对象和原型。我知道,这就是为什么我说类只是语法糖,类是对象。最后一个答案中选择A的问题是,它只存储长度一次,并且当数组的长度改变时不会改变。无论何时从数组中添加或删除项,甚至将变量重新分配给其他数组,都必须自己更新变量。对于选项B,它本质上使用了length
属性的getter,该属性将按预期进行更新。我错过了从一个分离的变量中提取lenght的部分。将修改我的答案。