javascript - 如何从同一类javascript中的方法返回对象数组

标签 javascript class object

我是 JavaScript 新手。我想要一个类方法返回由同一类实例化的对象数组。如何做到这一点?

目前,基本上以下是我所讨论的代码的一般结构。

class myClass{
   constructor(name,password,emailid,id) {
    this.name = name;
    this.password = password;
    this.emailid = emailid;
    this.id = id;
  }

  asyncMethod = async()=> {
     //method returns array of objects of same class
  }
}

最佳答案

在类实例化(构造函数 init)时,将当前实例推送到类的 static Array .
通过使用 userInstance.getInstances()(← 这是不好的做法!进一步阅读)或 User.instances

检索所有实例>

class User {

  static instances = [];

  constructor(name, password, emailid, id) {
    this.name = name;
    this.password = password;
    this.emailid = emailid;
    this.id = id;
    User.instances.push(this); // Push instance into static Class's property
  }

  getInstances() {
    return User.instances; // Return array of instances of same Class
  }
}

const A = new User("Cada", "123", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cffdcffb2ff" rel="noreferrer noopener nofollow">[email protected]</a>", 1);
const B = new User("Roko", "234", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7c4e7c207c" rel="noreferrer noopener nofollow">[email protected]</a>", 2);
const C = new User("John", "345", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="395379531753" rel="noreferrer noopener nofollow">[email protected]</a>", 3);

console.log(A.name, B.name, C.name); // "Cada", "Roko", "John"
console.log(A.getInstances());  // [User, User, User]
console.log(myClass.instances); // [User, User, User]

密切相关的答案:https://stackoverflow.com/a/61014433/383904

我无法比 MDN 更好地解释 static 关键字。 :

Neither static methods nor static properties can be called on instances of the class. Instead, they're called on the class itself. (*ac: myClass.instances in the above demo)
Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.


将类的内部公开给实例通常是一种不好的做法
例如,User 实例不应该能够从其 Class 构造函数继承整个用户列表。
下面是一个使用Static Private # 和 Class Getters get 的轻微变体:

class User {

  static #_instances = []; // Static and private

  constructor(userData) {
    Object.assign(this, userData);
    User.#_instances.push(this);
  }

  static get instances() { // Static to Class
    return [...User.#_instances]; // exposes private (as immutable)
  }
}

const A = new User({name:"Cada", password:"123", emailid:"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a092a094409" rel="noreferrer noopener nofollow">[email protected]</a>", id:1});
const B = new User({name:"Roko", password:"234", emailid:"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9dbe9db87db" rel="noreferrer noopener nofollow">[email protected]</a>", id:2});

console.log(A.name, B.name); // "Cada", "Roko",

console.log(A.instances);            // undefined 😭
console.log(User.instances);         // [User, User] 😎

User.instances.push({name:"EVIL!"}); // 😈  
console.log(User.instances);         // [User, User] 😎

关于javascript - 如何从同一类javascript中的方法返回对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71696605/

相关文章:

cocoa - 需要帮助在 cocoa 中实现进程间通信

javascript - 按下按钮时更改 li 元素类,并在该 css 类中更改背景颜色

ruby - 简单的ruby语法问题

javascript - 如何在js中检查一个表是否有重复的行列?

java - 如何访问我只通过字符串名称知道的类的类字段?

javascript - 在 Kendo 编辑器工具中使用 jquery 中的类创建按钮单击事件

java - 向对象添加唯一 ID (UUID)

javascript - 将对象推送到数组时,新行会附加在末尾

javascript - Angular-gettext 翻译服务似乎不起作用。

javascript - useCallback 对当前组件做了什么?