【发布时间】:2018-08-19 10:55:46
【问题描述】:
我想在调用类的构造函数时创建对象的数组或对象?我的意思是,每次我调用类构造函数时,它都会将新对象添加到数组或对象中。
f.e.
let blocks = {};
class Block(){
constructor(X,Y,width,height){
this.X = X;
this.Y = Y;
...
and now I would like to add this created object to "blocks"
something like: blocks.push(and this object here)
}
}
我怎样才能实现这样的东西?还是应该是另一个功能?
【问题讨论】:
-
您可以在类中添加另一个方法并在构造函数中调用此方法。
标签: javascript class oop ecmascript-6