Monthly Archives: April 2022

THE FUNCTION

FIRST CLASS FUNCTION In this post we will talk about the fundamental characteristics of Javascript functions. We will talk about very important concepts such as abstraction, encapsulation which was used in ES5 called IIFE, the keyword this etc. Translating this definition with regards to JS functions it means that everything we can do with strings numbers etc. we can do it with functions. Just as we can assign a variable to a number, a string etc. so we can assign that variable to a function. Let's see a code example to clarify these concepts. [...]

By |2024-11-11T18:26:11+00:00April 14, 2022|0 Comments

THE PARAMETERS OF THE FUNCTIONS

ARGUMENTS, REST AND SPREAD Parameters passed to a function are also available through the arguments object, it is an array-like object with which it is possible to access the elements passed as input. The following code clarifies this topic. THE PARAMETERS OF THE FUNCTIONS A JavaScript function does not perform any checks on parameter values (arguments). Earlier in this tutorial, you learned that functions can have parameters: function functionName(parameter1, parameter2, parameter3) {   // code to be executed } The function parameters are the names listed in the function definition. The arguments to the function are the real values [...]

By |2024-11-11T18:26:42+00:00April 15, 2022|0 Comments

THE JAVASCRIPT CLOUSURES

JAVASCRIPT CLOUSURES JavaScript variables can belong to the local or global scope. They can be made local (private) with closures. GLOBAL VARIABLES A function can access all variables defined within the function, like this: function myFunction() {   let a = 4;   return a * a; } But a function can also access variables defined outside the function, like this: let a = 4; function myFunction() {   return a * a; } In the last example, a is a global variable. In a web page, global variables belong to the window object. Global variables can be used (and modified) by all scripts in the page (and window). In [...]

By |2024-11-11T18:28:15+00:00April 16, 2022|0 Comments

DEFINITION OF OBJECT

DEFINITION OF OBJECT In JavaScript, objects are important. If you understand objects, you understand JavaScript. In this language, almost "everything" is an object.   Booleans can be objects (if defined with the new keyword) Numbers can be objects (if defined with the new keyword) Strings can be objects (if defined with the new keyword) Dates are always objects Math is an object Regular expressions are always objects Arrays are always objects Functions are always objects Objects are always objects All JavaScript values, except primitive values, are objects. PRIMITIVE VALUES A primitive value is a value that has no properties [...]

By |2024-11-11T18:28:51+00:00April 2, 2022|0 Comments

PROPERTY OF JAVASCRIPT OBJECTS

PROPERTY OF THE OBJECTS Properties are the most important part of any JavaScript object. An object is a collection of unsorted properties. Properties can usually be edited, added, and deleted, but some are read-only. The syntax for accessing the property of an object is: objectName.property      // person.age or: objectName["property"]   // person["age"] or: objectName[expression]   // x = "age"; person[x] The expression must return a property name. const person = {   firstName: "John",   lastName: "Doe",   age: 50,   eyeColor: "blue" }; person.firstname + " is " + person.age + " years old."; person["firstname"] + " is " + person["age"] + " years old."; FOR IN LOOP The JavaScript for ... in statement iterates through the properties of an [...]

By |2024-11-11T18:31:10+00:00April 3, 2022|0 Comments

METHODS OF JAVASCRIPT OBJECTS

METHODS OF OBJECTS fullName is an example of "javascript object methods". const person = {   firstName: "John",   lastName: "Doe",   id: 5566,   fullName: function() {     return this.firstName + " " + this.lastName;   } }; As for the keyword this, refer to this post (the keyword this). JavaScript methods are actions that can be performed on objects. A JavaScript method is a property that contains a function definition. Methods are functions stored as properties of the object. ACCESS THE METHODS An object method is accessed with the following syntax: objectName.methodName() You usually describe fullName () as a method of the person object and fullName as a property. The [...]

By |2024-11-11T18:31:59+00:00April 4, 2022|0 Comments

ACCESS TO JAVASCRIPT OBJECTS

ACCESS TO JAVASCRIPT OBJECTS ECMAScript 5 (ES5 2009) introduced Getter and Setter. They allow you to define access objects (calculated properties). THE KEYWORD GET This example uses a lang property to get the value of the language property. // Create an object: const person = {   firstName: "John",   lastName: "Doe",   language: "en",   get lang() {     return this.language;   } }; // Display data from the object using a getter: document.getElementById("demo").innerHTML = person.lang; THE KEYWORD SET This example uses a lang property to set the value of the language property. const person = {   firstName: "John",   lastName: "Doe",   language: "",   set lang(lang) {     this.language = [...]

By |2024-11-11T18:32:52+00:00April 5, 2022|0 Comments

CLASS DEFINITION

CLASS DEFINITION In object-oriented programming, a class is a construct of a programming language used as a model for creating objects. The model  includes attributes and methods that will be shared by all objects created (instances) from the class. An "object" is, in fact, the instance of a class. In modern JavaScript there is a more advanced "class" construct, which introduces very useful new possibilities for object-oriented programming. THE SYNTAX OF CLASS class MyClass {   // metodi della classe   constructor() { ... }   method1() { ... }   method2() { ... }   method3() { ... [...]

By |2024-11-11T18:34:52+00:00April 22, 2022|0 Comments

INHERITANCE IN JAVASCRIPT

Inheritance in javascript is a feature that allows a class to extend the properties of other classes. THE KEYWORD EXTENDS Let's assume we have an Animal class: class Animal {   constructor(name) {     this.speed = 0;     this.name = name;   }   run(speed) {     this.speed = speed;     alert(`${this.name} runs with speed ${this.speed}.`);   }   stop() {     this.speed = 0;     alert(`${this.name} stands still.`);   } } let animal = new Animal("My animal"); Here we see how to represent the Animal object and the Animal class graphically: We may want to [...]

By |2024-11-11T18:36:17+00:00April 24, 2022|0 Comments

STATIC METHODS

STATIC METHODS We can also assign methods to the classes themselves, not just their "prototype". These methods are called static. Inside the class, these are preceded by the keyword static, as we can see in the example: class User {   static staticMethod() {     alert(this === User);   } } User.staticMethod(); // true The value of this in the User.staticMethod() call is represented by the constructor of the User class (the rule of the object before the dot). Usually, static methods are used to represent functions that belong to the class, but not to a particular object. For example, we may have objects of type Article and need a [...]

By |2024-11-11T18:37:06+00:00April 25, 2022|0 Comments
Go to Top