I keep getting the error: “Oops, try again. Your code caused the following error when the Rectangle constructor was called: RangeError: Maximum call stack size exceeded” but I can’t find what the issue is or what the error means.
function Rectangle(height, width) {
this.height = height;
this.width = width;
this.calcArea = function() {
return this.height * this.width;
};
// put our perimeter function here!
this.calcPerimeter = function() {
return 2 * this.height + 2 * this.width;
};
var rex = new Rectangle(7,3);
var area = rex.calcArea();
var perimeter = rex.calcPerimeter();
};
Closed the function block in the wrong place.
function Rectangle(height, width) {
this.height = height;
this.width = width;
this.calcArea = function() {
return this.height * this.width;
};
// put our perimeter function here!
this.calcPerimeter = function() {
return 2 * this.height + 2 * this.width;
};
} // <--- close it here
var rex = new Rectangle(7,3);
var area = rex.calcArea();
var perimeter = rex.calcPerimeter();
console.log(rex, area, perimeter);
1 Like
Hii @cucumkid, I just want to ask that in constructor… we can define function like:
function Rectangle (height, width){… } // Here the word “R” is capital in “Rectangle”.
AND This way:
function rectangle (height, width){… } // Here the word “R” is small in “Rectangle”.
as far as I have checked these both ways shows me no error… but earlier I’ve learned that to define function first word should be small… but in constructor they always use to define function with first letter capital. So in real coding we should define function with 1st letter capital or small?
Please help me with the solution.
Thanks in advance.
Hi @arcjumper22033, that’s a good question. Technically there are two facts to know, Commonly used legitimate characters for a function are starts with any letter (upper or lower case) can be followed by numbers, or can start with ‘_’ or ‘$’, can’t start with a number or other symbol. And upper and lower case are treated as different.
So function Add() and function add() would be treated as different functions. The potential for mistakes with that goes up so the advice is always use clearly different names. Also even though ‘_’ and ’' are permitted aim to avoid them. That is because of convention rather than rule, _ has been a convention to mark private variables and is used by jQuery and a couple of other libraries.
With function names, there is also a convention.
Lower case marks a general function or a method:
function square(x,y) {
return x * y;
}
Upper case makes a constructor for objects
function Square(x,y) {
this.x = x;
this.y = y;
}
var mySq = new Square(4,6);
The convention to use capitals to mark a constructor is to help avoid confusion and to make constructor functions appear ‘class like’ for when you do object orientated programming. It isn’t a programming rule, JavaScript won’t cause errors over it, but it’s useful to know especially when you share code with others.
2 Likes
Thank you so much for clearing my confusion… actually earlier I was mixing function with constructor… but anyways thanks…
1 Like