Constructor not working?


I get 'TypeError: Person is not a constructor'.

// Our Person constructor

var Person = function(name, age){
// Now we can make an array of people

var family = new Array();
family[0] = new Person('alice', 40);
family[1] = new Person('bob', 42);
family[2] = new Person('michelle', 8);
family[3] = new Person('timmy', 6);

// loop through our new array
for(Person in family){


Hello @krazykira,

The syntax for making a constructor is

function Constructor(arg1, arg2) {
    this.prop1 = arg1
    this.prop2 = arg2

What you have done is made a JavaScript function, which is a reusable block of code which performs certain operation on arguments and returns a result....



===== Datatype Array =========
What you must understand,
is that if you ( as an example )
create a array
for the Javascript-Interpreter
you have created an object of the prototype array.
and if you do a google search
javascript array
and choose
on the left hand side you will see
what information you can gather about the array



Methods ( pre-defined functions for Array operations )


========= looping over an array ===

The most common way to loop over all elements of an Array
is using the generic FOR-loop

for ([initialization]; [condition]; [final-expression]) {

You would
start at index zero,
continue as long as the index is =less than= the array.length,
and at each iteration increment / decrement the index-pointer

var myArray =["a",2,{anObjectKey: "anObjectValue"}];
for ( var index = 0, len = myArray.length; index < len ; index= index + 1 ) {
     // get access to the =element-Value= using the [bracket-notation]
     console.log( index + "==> "+myArray[index] );

== Do NOT use FOR-IN loop on an Array ==

You could look at an Array as it being an Object like

 myArray = {
      0: "a",
      1: 2,
      2: { anObjectKey: "anObjectValue"}

and with the FOR-IN loop

for (var key in myArray) {
     //getting access to the =associate property-Value= 
      //using the =key= in a [bracket-notation]
      console.log( key + "==>"+myArray[key] );

but do a google search
== discussions / opinions ==
javascript why is it a bad idea to use for-in on an Array
for instance
An array is an object,
and array elements are simply properties with the numeric index converted to string.
For example, arr[123] refers to a property "123" in the array object arr.

The for ... in construct
works on all objects, not just arrays,
and that is cause for confusion.

When somebody for ... in an array,
most often the programmer intends to iterate just all the items, even most likely in order.

The semantics is so similar to array iteration in other programming languages that it is very easy to get confused.
In JavaScript, this construct does not iterate array elements in order.
It iterates all

  • the array's properties
  • (including inherited prototype functions, any properties added to it, any other non-element properties added to it etc.),
  • and not in order at all.
  • It will even find the property "length".

This is most likely not what the programmer means, if he/she comes from another programming language.

That's why you shouldn't do it.
You shouldn't use a language construct
that looks like it does obvious things, but actually does things that are completely different.
It creates bugs that are very obscure and very difficult to find.
[end quote Stephen Chung]

the for-loop:


Thanks, all! I needed the help.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.