.push无法在构造函数中工作

Der*_*會功夫 1 javascript constructor

如果我做:

function a(){
    a.list.push(this);
}
a.list = [];

new a();
Run Code Online (Sandbox Code Playgroud)

a.list将在new a()调用时更新.但现在,

function Spark(ctx){
    console.log("created", this);
    Spark.list.push[this];
}
Spark.list = [];
Spark.max = 100;
Spark.createSparks = function(ctx){
    if(this.list.length < this.max){
        new Spark(ctx);
    }
};

Spark.createSparks();
Run Code Online (Sandbox Code Playgroud)

这里Spark.list.push执行没有任何问题,但Spark.list仍然是空的.这是为什么?

Ali*_*guy 5

你没有调用这个函数.

更改

Spark.list.push[this]
Run Code Online (Sandbox Code Playgroud)

Spark.list.push(this)
Run Code Online (Sandbox Code Playgroud)