Introducción a JavaScript
¿Por qué aprender JavaScript?
¡Hola Mundo! en Mac
¡Hola Mundo! en Windows
Anatomía de una variable
Tipos de datos en JavaScript
Creación de strings
Operadores aritméticos
Conversión de tipos: Type Casting y Coerción
Conversión de tipos explícita e implícita
Estructuras de Control y Lógica
Operadores de comparación
Operadores lógicos
Ejecución condicional: if
Ejercicio: Adivina el número
Ejecución condicional: switch
Loop: for
Loop: for of
Loop: for in
Loop: while
Loop: do while
Funciones y This
Anatomía de una función
Funciones vs Métodos
Funciones puras e impuras
Arrow function y enlace léxico
Contextos de ejecución y scope chain
¿Qué es Closure?
Preguntas a Desarrolladores Senior: ¿Por qué aprender Desarrollo Web?
Manipulación de Arrays
Introducción a Arrays
Mutabilidad e inmutabilidad de Arrays
Modificación básica del final con push( ), pop( )
Iteración con map( ) y forEach( )
Filtrado y reducción con filter( ) y reduce( )
Búsqueda de elementos con find( ) y findIndex( )
Crear copias con slice( )
Spread operator: casos de uso
Programación Orientada a Objetos
Anatomía de un Objeto
Trabajando con objetos en JavaScript
Función constructora
¿Qué es una clase?
Prototipos y herencias
Herencia en la práctica
Prototipos en la práctica
this en JavaScript
Proyecto: Crea una red social
Proyecto: Crea una red social parte 2
Asincronía en JavaScript
¿Cómo funciona el JavaScript Engine?
Promesas en JavaScript
Usando Async y await en JavaScript
For await of
¿Cómo funciona la web?
¿Cómo funciona HTTP?
Método GET en JavaScript
Método POST en JavaScript
Método DELETE en JavaScript
Importancia del id en el método DELETE
ECMAScript 6 y tus siguientes pasos
You don't have access to this class
Keep learning! Join and start boosting your career
When working with specific instances of a class in JavaScript, sometimes we need unique methods that were not defined in the original constructor class. To address this need, it is possible to add new methods directly to an instance. To accomplish this, we simply declare the method as a function associated with the instance.
dog1.newMethod = function() { console.log("This is a new method.");};
This approach adds the method only to dog1
, causing other instances to not have it. Although functional for specific scenarios, this method is neither elegant nor efficient if we want all instances to share the same behavior.
The prototype in JavaScript is a fundamental tool for sharing methods and properties across multiple instances of a constructor class. If we are looking to make a method available to every instance of a class, it must be added directly to the prototype of that class.
Perro.prototype.secondMethod = function() { console.log("This is another new method.");};
With this approach, any instance of Perro
, already existing or created in the future, will be able to access segundoMetodo
. This technique is essential for memory economy and efficiency, since shared functions are stored in a single location in memory.
The prototype chain is a key concept that explains how methods and properties are inherited in JavaScript. When trying to access a property or method of an object, JavaScript first looks in the object itself. If it doesn't find it, it moves up the prototype chain until it finds it or reaches null
.
Dog
.Dog
could be extended.This proximity system allows an efficient distribution of methods, optimizing resources without replicating functions in each instance.
To visually understand how a prototype chain works, we can inspect the prototype of an instance. We use Object.getPrototypeOf()
to follow the chain.
let prototypeActual = Object.getPrototypeOf(dog1);while (prototypeActual) { console.log(prototypeActual); prototypeActual = Object.getPrototypeOf(prototypeActual);}
This code traverses the prototype chain, showing how dog1
is bound to the Dog
prototype, then to Animal
, and finally to Object
.
This system is essential for taking full advantage of JavaScript's capabilities, allowing inheritance and shared methods without unnecessary duplication of data. Learning to use prototypes efficiently is an important step on every JavaScript developer's path - keep practicing and exploring to become a better programmer!
Contributions 48
Questions 3
Want to see more contributions, questions and answers from the community?