+ 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); }

4th Jun 2017, 6:44 PM
Borel T.
Borel T. - avatar
2 odpowiedzi
+ 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. * */
4th Jun 2017, 6:47 PM
Maz
Maz - avatar
+ 1
Awesome. Thanks alot!
4th Jun 2017, 6:52 PM
Borel T.
Borel T. - avatar