我是 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/