Hello,
New to JavaScript and I have an assignment where one piece of it needs to have the first name, last name, area code, and phone number when prompted by a function, once that info is had, it then needs to use the constructor function that is there to process the data and lastly use the data to output it with a show function. I have it to work with static information, but cant seem to figure what I am missing or how to make the prompt function information to feed to the constructor, I know when using the static variable it uses "new Function" but that seems to be my problem, I am stuck.
Please see the code below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lab03</title>
<script type="text/javascript">
function getinfo( ) {
//Prompting for users information
var first_name = prompt( "Please enter your first name: " );
var last_name = prompt( "Please enter your last name: " );
var area_code = prompt( "Please enter your area code: " );
var phone_number = prompt( "Please enter your phone number: " );
}
function show_person( ) {
//Displays the infomation
document.writeln( "<hr/>" );
document.writeln( "Name: " + this.last_name + ", " );
document.writeln( this.first_name + "<br/>" );
document.writeln( "Phone number: " + "(" + this.area_code + ") " + this.phone_number + "<br/>" );
document.writeln( "<hr/>" );
}
function Person( persons ) {
// Constructor function
this.first_name = persons.first_name || "";
this.last_name = persons.last_name || "";
this.area_code = persons.area_code || "";
this.phone_number = persons.phone_number || "";
this.show = show_person;
}
</script>
</head>
<body>
<script type="text/javascript">
getinfo();
//getinfo.show();
//testing with
var person1 = new Person( {first_name: "James", last_name: "Johnson", area_code: "918", phone_number: "688-3953"} );
person1.show();
</script>
</body>
</html>