+ 1
Trying to work on objects and functions
So I just got started with JS which is basically like C++ but its weak typing always leaves me wondering where I went wrong. I am having a problem with this code I tried after doing the objects lesson and it won't work. Any ideas? "use strict"; function person(name, color, age) { this.name = name; this.color = color; this.age = age; } function index() { var p1 = person("Jack", "yellow", 19); alert("This is just the test " + p1.name); }
2 Answers
+ 14
function person(name, color, age) {
this.name = name;
this.color = color;
this.age = age;
}
function index() {
var p1 = new person("Jack", "yellow", 19);
alert("This is just the test " + p1.name);
}
index();
/*
*
* 1. You did not call the index( ) function
*
* 2. An object from a constructor,
* must be created with
* the "new" keyword.
*
*/
+ 1
Awesome. Thanks alot!