javascript - How to add object to array in constructor -
I am programming in Javascript and I have created an array by the user's name and created a constructor for the user object. Every time a new object is created, I want to add it to the array.
var users = []; Function user (username, password) {this.username = username; This.password = password; Users [users.length-1] = this; }; Var = New User ('Joe', 'JoaA 100');
The above method does not work. How can I add objects to an orange in the constructor?
"user" is an empty array, that means its length is 0 when you kill the constructor . Therefore assigning "this" to length -1 will assign "this" to the "1" index ... so why is not she doing the job ... get rid of -1, or. ..
Use the javascript array.push () function instead of adding it to your array
See examples on the W3Schools site ...
Comments
Post a Comment