[英]How can i find specific new object of Class that has a constructor argument equal to input
I have a class called Country with 4 constructor parameters. 我有一个名为Country的类,带有4个构造函数参数。
I then create some new countries from that class with specified values. 然后,我使用指定的值从该类创建一些新的国家/地区。
My question is, how can i create a method that can find and return the object with a this.value equal to the input in the method? 我的问题是,如何创建一个方法,可以找到并返回一个this.value等于方法输入的对象?
class Country {
constructor(name, area, population, topdomain) {
this.name = name;
this.area = area;
this.population = population;
this.topdomain = topdomain;
}
static findCountry = domain => {
/*Here is where the magic should happen.
If domain is in any of the countries below, then it should return the country name.
*/
}
}
norway = new Country("Norway", 323802, 5320045, ".no");
sweden = new Country("Sweden", 450295, 9960487, ".se");
russia = new Country("Russia", 17098242, 142257519, ".ru");
china = new Country("China", 9596960, 1379302771, ".cn");
This function here should return "Norway": 这个函数应该返回“Norway”:
Country.findCountry(".no");
For that to work, the class has to keep a list of all the created instances. 要使其工作,该类必须保留所有已创建实例的列表。 As JS has no weak references, this will mean that none of the instances can ever be garbage collected (so be careful):
由于JS没有弱引用,这意味着没有任何实例可以被垃圾收集(所以要小心):
static instances = [];
constructor(/*...*/) {
/*...*/
Country.instances.push(this);
}
static findCountry = domain => {
return this.instances.find(country => country.domain === domain);
}
Don't come here to ask people to write your code ;) 不要来这里要求别人写你的代码;)
class Country {
constructor(name, area, population, topdomain) {
this.name = name;
this.area = area;
this.population = population;
this.topdomain = topdomain;
Country._ALL.push(this);
}
static findBy(key, value) {
let output = [];
for ( let i in Country._ALL) {
let c = Country._ALL[i];
if (c.hasOwnProperty(key) && c[key] === value)
output.push(c);
}
return output;
}
}
Country._ALL = [];
WARNING! 警告! ES6 classes do not support static variables like
static variable = [];
ES6类不支持静态变量,如
static variable = [];
If you want static class variables in ES6 you have to use ClassName.variable = [];
如果你想在ES6中使用静态类变量,你必须使用
ClassName.variable = [];
after the class declaration. 之后类的声明。
Your class doesn't know about the 4 objects you instantiated somewhere. 你的类不知道你在某处实例化的4个对象。 You need to put them in a collection (eg an array), and then explicitly reference that collection in your search method:
您需要将它们放在一个集合(例如数组)中,然后在搜索方法中显式引用该集合:
class Country {
constructor(name, area, population, topdomain) {
this.name = name;
this.area = area;
this.population = population;
this.topdomain = topdomain;
}
static findCountry(domain) {
return (knownCountries.find(country => country.topdomain == domain) || {}).name;
// ^^^^^^^^^^^^^^
}
}
const norway = new Country("Norway", 323802, 5320045, ".no");
const sweden = new Country("Sweden", 450295, 9960487, ".se");
const russia = new Country("Russia", 17098242, 142257519, ".ru");
const china = new Country("China", 9596960, 1379302771, ".cn");
const knownCountries = [norway, sweden, russia, china];
// ^^^^^^^^^^^^^^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.