简体   繁体   English

如何找到具有构造函数参数等于输入的Class的特定新对象

[英]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.

相关问题 我怎样才能找到<a>一个特定的类,当它在</a><li> - How can i find if an <a> has a specific class when it is in an <li> 当输入字段具有特定值时,如何更改div的类? - How can i change the class of a div when input field has a specific value? 如何注释该参数是 class 并具有某些 static 方法? - How can I annotate that argument is a class and has certain static method? 当我需要使用带有参数的构造函数时,如何使用Rhino子类化(扩展)Java类? - How can I subclass (extend) a Java class using Rhino when I need to use a constructor that takes an argument? 如何解构对象并将结果作为参数传递给类构造函数? - how to destructure an object and pass the result as argument in a class constructor? 如何选择仅具有特定类别的元素? - How can I select an element which has only a specific class? 如何检测标签是否具有特定类别? - How can I detect if a label has a specific class? jQuery-如何确定特定类是否已切换? - JQuery - How can I determine if a specific class has been Toggled? 我怎么知道一个元素是否有一个特定的 JavaScript 类? - How can I know if an element has a specific class with JavaScript? 如何在字符串中找到特定单词并添加类(Javascript) - How I can find specific words in the string and add class (Javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM