my JavaScript object has an array in it but when I try to return an element of the array I get the function as a string instead of the value of the array element.
My code is
function Car(make, model){
this.make = make;
this.model = model;
this.dimensions = [4, 3, 1.8];
this.carLength = function(){ return this.dimensions[0]; };
}
var c = new Car("Ford", "Escort");
alert(c.make);
alert(c.model);
alert(c.dimensions[0]);
alert(c.carLength);
The first 3 alerts show the data expected, ("Ford", "Escort" and 4), the forth one displays the following output
function(){ return this.dimensions[0]; }
Why is the function being listed and not executed?
CarInstance.carLengthis a method. You have to call it, or you'll just get the function.console.log(c.carLength())alert(c.carLength())