When I console.log, this code:
var Student = function(name, address, gpa){
console.log(this);
this.name = name;
this.address = address;
this.gpa = gpa;
console.log("name is equal to " + name);
console.log("address is equal to " + address);
console.log("gpa is equal to " + gpa);
};
var studentCall = [
new Student ({
name: "Marshae Hannor",
address:{
street: "345 Main St",
city: "Smyrna",
state: "GA"},
gpa: [2.5, 3.5, 4.0]}),
new Student ({
name: "Vernon Owens",
address:{
street: "439 Serious St",
city: "Baltimore",
state: "MD"},
gpa: [3.5, 3.2, 3.7]})
];
this is what I get in the console.log
Object {} main2.js (line 39) name is equal to [object Object] main2.js (line 44) address is equal to undefined main2.js (line 45) gpa is equal to undefined main2.js (line 46) Object {} main2.js (line 39) name is equal to [object Object] main2.js (line 44) address is equal to undefined main2.js (line 45) gpa is equal to undefined
Can someone help me understand what Im doing incorrectly. Thanks
In your calls to Student
, you're passing in a single argument, which is an object that looks like this:
{
name: "Marshae Hannor",
address:{
street: "345 Main St",
city: "Smyrna",
state: "GA"},
gpa: [2.5, 3.5, 4.0]
}
But your Student
function expects to receive three discrete arguments. So you'd call it without the {
and }
and without giving the names of the arguments:
var studentCall = [
new Student (
"Marshae Hannor",
{
street: "345 Main St",
city: "Smyrna",
state: "GA"},
[2.5, 3.5, 4.0]),
new Student (
"Vernon Owens",
{
street: "439 Serious St",
city: "Baltimore",
state: "MD"},
[3.5, 3.2, 3.7])
];
Or , modify Student
to expect to receive just a single object with those properties:
var Student = function(obj){
console.log(this);
this.name = obj.name;
this.address = obj.address;
this.gpa = obj.gpa;
console.log("name is equal to " + this.name);
console.log("address is equal to " + this.address);
console.log("gpa is equal to " + this.gpa);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.