我想獲得一個函數,提示用戶輸入信息,然後將該信息傳遞給對象。到目前爲止,它似乎沒有這樣做。 Javascript-從函數返回一個對象
// my object constructor
var Person = function (firstName, lastName, areaCode, phone) {
this.firstName = firstName;
this.lastName = lastName;
this.areaCode = areaCode;
this.phone = phone;
}
// my function to get user info
function getInfo() {
firstName = prompt("What is your first name: ");
lastName = prompt("What is your last name: ");
areaCode = prompt("What is your area code: ");
phone = prompt("What is your phone number: ");
var guy = Person(firstName, lastName, areaCode, phone);
return guy;
}
// calling the function
getInfo();
// test to see if it actually worked
document.writeln(guy.firstName);
你缺少 「新的」 變種人=新的Person(名字,姓氏,AREACODE,電話); – 2014-09-21 15:57:23
另外,您沒有使用返回值。 – SLaks 2014-09-21 15:59:43