TypeError: Cannot read properties of undefined (reading ‘xxx’)
This error occurs when you try to access a property of an object that is undefined.
1 | let person = undefined; |
ReferenceError: a is not defined
This is the most common error in JavaScript. It means that you are trying to use a variable that has not been declared.
1 | console.log(a); // Uncaught ReferenceError: a is not defined |
ReferenceError: Cannot access ‘a’ before initialization
This error occurs when you try to access a variable before it is declared. After ES6, this means you touch the (TDZ)temporal dead zone. temporal dead zone is the zone between the start of the block scope and the actual declaration of the variable.
1 | console.log(a); // Uncaught ReferenceError: Cannot access 'a' before initialization |
TypeError: Assignment to constant variable.
This error occurs when you try to reassign a value to a constant variable.
1 | const a = 1; |
RangeError: Maximum call stack size exceeded
See the following code, why it cause the RangeError: Maximum call stack size exceeded
error?
1 | class Person { |
The error is caused by the recursive call to the setter of name.
person.name = 'Philip';
will call the setterset name(value)
.- Inside the setter,
this.name = value;
will call the setter again. that’s an infinite loop.
How to fix it?
We can just use another name for the property, like _name
.
1 | class Person { |
In this way,
person.name = 'Philip';
will call the setterset name(value)
- Inside the setter function, we set the value to
_name
property, not thename
property. So it will not cause the infinite loop.
You can find the details of getter
and setter
from here.