From 1109132f09d75da9a28b649c7677bb6ce07c40c0 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:41:45 -0500 Subject: initial commit --- .../fa/web/javascript/reference/errors/index.html | 31 ++++++ .../reference/errors/too_much_recursion/index.html | 114 +++++++++++++++++++++ .../reference/errors/unexpected_token/index.html | 84 +++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 files/fa/web/javascript/reference/errors/index.html create mode 100644 files/fa/web/javascript/reference/errors/too_much_recursion/index.html create mode 100644 files/fa/web/javascript/reference/errors/unexpected_token/index.html (limited to 'files/fa/web/javascript/reference/errors') diff --git a/files/fa/web/javascript/reference/errors/index.html b/files/fa/web/javascript/reference/errors/index.html new file mode 100644 index 0000000000..c295fccea6 --- /dev/null +++ b/files/fa/web/javascript/reference/errors/index.html @@ -0,0 +1,31 @@ +--- +title: JavaScript error reference +slug: Web/JavaScript/Reference/Errors +tags: + - Debugging + - Error + - Errors + - Exception + - JavaScript + - NeedsTranslation + - TopicStub + - exceptions +translation_of: Web/JavaScript/Reference/Errors +--- +

{{jsSidebar("Errors")}}

+ +

Below, you'll find a list of errors which are thrown by JavaScript. These errors can be a helpful debugging aid, but the reported problem isn't always immediately clear. The pages below will provide additional details about these errors. Each error is an object based upon the {{jsxref("Error")}} object, and has a name and a message.

+ +

Errors displayed in the Web console may include a link to the corresponding page below to help you quickly comprehend the problem in your code.

+ +

List of errors

+ +

In this list, each page is listed by name (the type of error) and message (a more detailed human-readable error message). Together, these two properties provide a starting point toward understanding and resolving the error. For more information, follow the links below!

+ +

{{ListSubPages("/en-US/docs/Web/JavaScript/Reference/Errors")}}

+ +

See also

+ + diff --git a/files/fa/web/javascript/reference/errors/too_much_recursion/index.html b/files/fa/web/javascript/reference/errors/too_much_recursion/index.html new file mode 100644 index 0000000000..02a8d54c45 --- /dev/null +++ b/files/fa/web/javascript/reference/errors/too_much_recursion/index.html @@ -0,0 +1,114 @@ +--- +title: 'InternalError: too much recursion' +slug: Web/JavaScript/Reference/Errors/Too_much_recursion +translation_of: Web/JavaScript/Reference/Errors/Too_much_recursion +--- +
{{jsSidebar("Errors")}}
+ +

Message

+ +
Error: Out of stack space (Edge)
+InternalError: too much recursion (Firefox)
+RangeError: Maximum call stack size exceeded (Chrome)
+
+ +

Error type

+ +

{{jsxref("InternalError")}}.

+ +

What went wrong?

+ +

A function that calls itself is called a recursive function. Once a condition is met, the function stops calling itself. This is called a base case.

+ +

In some ways, recursion is analogous to a loop. Both execute the same code multiple times, and both require a condition (to avoid an infinite loop, or rather, infinite recursion in this case). When there are too many function calls, or a function is missing a base case, JavaScript will throw this error.

+ +

Examples

+ +

This recursive function runs 10 times, as per the exit condition.

+ +
function loop(x) {
+  if (x >= 10) // "x >= 10" is the exit condition
+    return;
+  // do stuff
+  loop(x + 1); // the recursive call
+}
+loop(0);
+ +

Setting this condition to an extremely high value, won't work:

+ +
function loop(x) {
+  if (x >= 1000000000000)
+    return;
+  // do stuff
+  loop(x + 1);
+}
+loop(0);
+
+// InternalError: too much recursion
+ +

This recursive function is missing a base case. As there is no exit condition, the function will call itself infinitely.

+ +
function loop(x) {
+ // The base case is missing
+
+loop(x + 1); // Recursive call
+}
+
+loop(0);
+
+// InternalError: too much recursion
+ +

Class error: too much recursion

+ +
class Person{
+	constructor(){}
+	set name(name){
+		this.name = name; // Recursive call
+	}
+}
+
+
+const tony = new Person();
+tony.name = "Tonisha"; // InternalError: too much recursion
+
+ +

When a value is assigned to the property name (this.name = name;) JavaScript needs to set that property. When this happens, the setter function is triggered.

+ +
set name(name){
+	this.name = name; // Recursive call
+}
+
+ +
+

In this example when the setter is triggered, it is told to do the same thing again: to set the same property that it is meant to handle. This causes the function to call itself, again and again, making it infinitely recursive.

+
+ +

This issue also appears if the same variable is used in the getter.

+ +
get name(){
+	return this.name; // Recursive call
+}
+
+ +

To avoid this problem, make sure that the property being assigned to inside the setter function is different from the one that initially triggered the setter.The same goes for the getter.

+ +
class Person{
+	constructor(){}
+	set name(name){
+		this._name = name;
+	}
+	get name(){
+		return this._name;
+	}
+}
+const tony = new Person();
+tony.name = "Tonisha";
+console.log(tony);
+
+ +

See also

+ + diff --git a/files/fa/web/javascript/reference/errors/unexpected_token/index.html b/files/fa/web/javascript/reference/errors/unexpected_token/index.html new file mode 100644 index 0000000000..77fa2e06c5 --- /dev/null +++ b/files/fa/web/javascript/reference/errors/unexpected_token/index.html @@ -0,0 +1,84 @@ +--- +title: 'SyntaxError: Unexpected token' +slug: Web/JavaScript/Reference/Errors/Unexpected_token +translation_of: Web/JavaScript/Reference/Errors/Unexpected_token +--- +
+ +
+ +
+

{{jsSidebar("Errors")}}

+
+ +

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

+ +

Message

+ +
SyntaxError: expected expression, got "x"
+SyntaxError: expected property name, got "x"
+SyntaxError: expected target, got "x"
+SyntaxError: expected rest argument name, got "x"
+SyntaxError: expected closing parenthesis, got "x"
+SyntaxError: expected '=>' after argument list, got "x"
+
+ +

Error type

+ +

{{jsxref("SyntaxError")}}

+ +

What went wrong?

+ +

A specific language construct was expected, but something else was provided. This might be a simple typo.

+ +

Examples

+ +

Expression expected

+ +

For example, when chaining expressions, trailing commas are not allowed.

+ +
for (let i = 0; i < 5,; ++i) {
+  console.log(i);
+}
+// SyntaxError: expected expression, got ')'
+
+ +

Correct would be omitting the comma or adding another expression:

+ +
for (let i = 0; i < 5; ++i) {
+  console.log(i);
+}
+
+ +

Not enough brackets

+ +

Sometimes, you leave out brackets around if statements:

+ +
function round(n, upperBound, lowerBound){
+  if(n > upperBound) || (n < lowerBound){
+    throw 'Number ' + String(n) + ' is more than ' + String(upperBound) + ' or less than ' + String(lowerBound);
+  }else if(n < ((upperBound + lowerBound)/2)){
+    return lowerBound;
+  }else{
+    return upperBound;
+  }
+} // SyntaxError: expected expression, got '||'
+ +

The brackets may look correct at first, but note how the || is outside the brackets. Correct would be putting brackets around the ||:

+ +
function round(n, upperBound, lowerBound){
+  if((n > upperBound) || (n < lowerBound)){
+    throw 'Number ' + String(n) + ' is more than ' + String(upperBound) + ' or less than ' + String(lowerBound);
+  }else if(n < ((upperBound + lowerBound)/2)){
+    return lowerBound;
+  }else{
+    return upperBound;
+  }
+}
+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf