我想获得一个函数,提示用户输入信息,然后将该信息传递给对象。到目前为止,它似乎没有这样做。 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