DEV Community

BekmuhammadDev
BekmuhammadDev

Posted on

constructor function / Errors

  • constructor function
  • Errors
  • debugger keyword

constructor function:

A constructor function is a special function used to create objects in JavaScript.

A constructor function is created like this:

function Car(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;
}
Enter fullscreen mode Exit fullscreen mode

A constructor function named Car is created here. A constructor function is used to create a new object and usually starts with an uppercase letter.
This function has make, model, and year parameters, which represent the properties of the newly created object.

The this keyword specifies the object being created:
this Keyword:
The value of the this keyword depends on how you call the function. In constructor functions, this refers to the newly created object. Within methods, this refers to the object on which the method was called.

Create a new object:

let myCar = new Car('Toyota', 'Corolla', 2020);
Enter fullscreen mode Exit fullscreen mode

Here the Car constructor function is called using the new keyword and a new object is created. This call performs the following tasks:

  • A new empty object is created.
  • The created object is attached to this context.
  • The Car function populates a new object with this: The make, model, and year properties take the values ​​Toyota, Corolla, and 2020.
  • The constructor function automatically returns the newly created object and attaches it to the myCar variable.

Export object properties to cansole:

console.log(myCar.make); 
Enter fullscreen mode Exit fullscreen mode

Here, after the myCar variable is created, we output the output via console.log to access its make property. This code outputs the value Toyota to the console because the make property of the myCar object is set to Toyota.

JAVASCRIPT ERRORS
types of errors

  • ReferenceError
  • SyntaxError
  • TypeError
  • URIError
  • EvalError
  • InternalError:

1-ReferenceError:
A ReferenceError occurs when a particular variable or function does not exist or is not defined.

console.log(test); 
Enter fullscreen mode Exit fullscreen mode

cansole:

Image description

2-SyntaxError:
SyntaxError Occurs when there is a syntax error in JavaScript code. These errors represent code violations.

a =; 5; // 
Enter fullscreen mode Exit fullscreen mode

Here a =; is written incorrectly because the = operator is not used correctly.

cansole:

Image description

3-TypeError:
TypeError occurs when a particular action or operation is performed against a value of the wrong type.

"abc".toFixed(5); 
Enter fullscreen mode Exit fullscreen mode

Here the toFixed method only applies to numeric values, but it is applied to string values.

cansole:

Image description

4-URIError:
URIError occurs in case of wrong usage related to URI (Uniform Resource Identifier). These errors can occur when the decodeURI(), decodeURIComponent(), encodeURI(), and encodeURIComponent() functions are called with incorrect parameters.

decodeURIComponent('%'); 
Enter fullscreen mode Exit fullscreen mode

An error occurs here because the % sign is invalid.

cansole:

Image description

5-EvalError:
An EvalError can occur as a result of an incorrectly used eval() function in a program. However, since ES5, EvalError is almost deprecated and rarely encountered.

eval("foo bar"); 
Enter fullscreen mode Exit fullscreen mode

After ES5, EvalError is usually replaced by other errors.

cansole:

Image description

6-InternalError:
InternalError is caused by an internal problem or limitation in the JavaScript engine. These errors are rare and often occur when the depth of recursion is exceeded or when other internal errors occur within the JavaScript interpreter.

function recurse() {
    recurse();
}
recurse();
Enter fullscreen mode Exit fullscreen mode

Here, the recursion depth is exceeded due to the function calling itself indefinitely.

cansole:

Image description

A try-catch block is used to catch and handle errors in JavaScript. Correct handling and handling of errors is important to ensure the stability of the application.

debugging

In JavaScript, the debugger keyword is used to analyze the code step by step, simplifying the process. This keyword allows you to stop and analyze the status of all code written in javascript, functions, code running in the browser or other debugging tools.

Debugging tasks:

The debugger keyword stops reading the code at the specified location and invokes the debugger. This is especially useful for understanding complex codes and detecting errors.
Where the code is interrupted, variables can be observed and their values ​​analyzed. This makes it easier to quickly find and fix errors. We can check all the written codes one by one and see the result.

The debugger keyword works:

function name1() {
    console.log('name1');
}

function name2() {
    console.log('name2');
}

function name3() {
    console.log('name3');
}

debugger

name1();

debugger 

name2();

debugger

name3();
Enter fullscreen mode Exit fullscreen mode

In this code, we can analyze the functions written from the top of the functions called by calling the debug keyword and moving down from the top, that is, we can analyze the functions and results.

debugging:

Image description

cansole:

Image description

Precautions When Using the Debugger Keyword:

Remove in production: The debugger keyword should be removed in a production environment. Otherwise, it may cause inconvenience to users.

Usage Limitation: Use the debugger keyword only where necessary and in short-term analysis processes. You shouldn't have too many debugger keywords in your code.

The debugger keyword is a powerful tool in JavaScript for stopping code execution, observing variables, and finding errors. With this keyword, you can analyze the code step by step and quickly identify and fix complex problems. It is important to know how to use the debugger keyword and its benefits to make the debugging process efficient.

I apologize for any mistakes

Top comments (0)