Javascript公共成员无法访问

And*_*ius 0 javascript object member

我有一个非常简单的结构:

var FORMS = [];

function FormObject(type)
{
    this.FormId = FORMS.length;
    //alert(this.FormId); returns results 0 and 1 respectively.
    this.Type = type;
    FORMS.push(FormObject); 
    this.generate = generate();
}

function generate()
{
    return 5;
}
Run Code Online (Sandbox Code Playgroud)

然后我做这样的事情:

var new_form = new FormObject('fruit');
var another  = new FormObject('vegetable');
alert(another.FormId);//as expected, I get 1 as a result
Run Code Online (Sandbox Code Playgroud)

然后最后尝试做这样的事情:

alert(FORMS.length);//result is 2 so I assume the objects got created successfully
alert(FORMS[0]);//prints the whole code of the constructor into the dialog box
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试这样的事情时:

alert(FORMS[0].FormId);//result is undefined!!!
alert(FORMS[0].generate());//it shows an error that the object does not have such method
Run Code Online (Sandbox Code Playgroud)

为什么不定义?我试着阅读http://javascript.crockford.com/private.html,它说:

Patterns

Public

function Constructor(...) {
    this.membername = value;
}
Constructor.prototype.membername = value;
Run Code Online (Sandbox Code Playgroud)

Dav*_*ing 6

更换:

FORMS.push(FormObject); 
Run Code Online (Sandbox Code Playgroud)

FORMS.push(this);
Run Code Online (Sandbox Code Playgroud)

当您按下FormObject对象时,您正在推送构造函数,而不是实例.