33 Methods (help)


#1

function triangle (base,height) {
this.base = base;
this.height = height;
this.area = function (){
return 1/2 * this.base * this.height;

};

};

console.log (triangle (500,10));

this is my code but its returning undefined can someone explain to me why


#2

The triangle function is a custom object constructor, so we don't call on it like we would a normal function. Instead, we invoke it with the new operator to instantiate a custom instance.

function Triangle(base, height){
    // constructor statements
}

var triangle = new Triangle(50, 10);
console.log(triangle.area());        // 250

#3

can you tell me why after triangle.area you have parentheses?


#4

Because area() is a function. A method, actually, of the Triangle class. We need to call it like we would any function.

triangle.area is a reference to the function object, a string. It does not invoke the function so there is no action or return. Think of the reference as a copy of the code.