[英]How to push new constructor properties to array?
假設我有構造函數 person 並使用 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);
推送對象,而不是它們的名字。
personArray.push(sam, john);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.