假设我有构造函数的人并使用new创建相同类型的对象我现在想将这些值推送到数组。我怎么能用javaScript做到这一点?
ctrl.js
var personArray = [];
function person(firstname,lastname){
this.firstname = firstname;
this.lastname= lastname;
this.fullName = function (){
return this.firstname + " " + this.lastname
}
}
var sam = new person("Sam","Martin");
var john = new person("John","Martin");
personArray.push(sam.fullName(),john.fullName());
console.log(personArray);
答案 0 :(得分:1)
推送对象,而不是他们的名字。
personArray.push(sam, john);