JS Concepts Flashcards
JS Tricky parts
CLOSURE
The ability for inner functions to remember variables defined in outer functions, long after the outer function has returned. Useful for encapsulating logic and private variables.
IIFE
Immediately Invoked Function Expression
A function which is invoked immediately. Used for encapsulating variables and functions in a scope, preventing them from being manipulated or accessed from outside.
PROTOTYPE
Prototypes are mechanisms that help JavaScript objects inherit features from one another.
The new keyword
The new keyword does four things:
1. Creates an empty object
2. Sets the keyword ‘this’ to be that object
3. Returns the object - return this
4. Creates a link to the object’s prototype
MDN: The new operator lets developers create an instance of a user-defined object type or of one of the built-in objects types that has a constructor function.
This keyword
- Global context: which is window
- The function context:
Prototypical Inheritance
Prototypical inheritance is a key feature of JavaScript that allows objects to inherit properties and methods from other objects. In JavaScript, each object has an internal property called [[Prototype]] that points to another object, known as its prototype. If a property or method is not found on an object, JavaScript looks up the prototype chain until it finds the property or until the end of the chain is reached.
How Prototypical Inheritance Works:
Prototype Chain:
Every object in JavaScript has a prototype, which is itself an object, forming a chain of prototypes.
The chain is traversed during property or method lookups.
Object Creation:
Objects can be created using constructor functions, object literals, or other means.
prototype Property:
Functions have a prototype property, which is used as the prototype for objects created by that function.
Object.create() Method:
The Object.create() method is used to create a new object with a specified prototype.
**********
// Constructor function
function Animal(name) {
this.name = name;
}
// Adding a method to the prototype
Animal.prototype.sayHello = function() {
console.log(Hello, I'm ${this.name}
);
};
// Creating objects using the constructor
const cat = new Animal(‘Cat’);
const dog = new Animal(‘Dog’);
cat.sayHello(); // Output: Hello, I’m Cat
dog.sayHello(); // Output: Hello, I’m Dog
// Prototypical inheritance
function Dog(name, breed) {
// Call the parent constructor
Animal.call(this, name);
this.breed = breed;
}
// Inherit from Animal’s prototype
Dog.prototype = Object.create(Animal.prototype);
// Adding a method specific to Dog
Dog.prototype.bark = function() {
console.log(‘Woof!’);
};
const germanShepherd = new Dog(‘Rex’, ‘German Shepherd’);
germanShepherd.sayHello(); // Output: Hello, I’m Rex
germanShepherd.bark(); // Output: Woof!
Object Methods
- Object.keys(obj): returns an array containing the names of all properties of an object.
- Object.values(obj): returns an array containing the keys
- Object.hasOwnProperty(prop): returns a boolean indicating if the object has a property with the specified name.
- Object.entries(obj): returns an array of arrays where each inner array has the key and value pair
- Delete object.property
- TO change a key, you can follow the example:
Person.firstname = person.name
Delete person.name
Set Methods
- Add(value): adds a new element to the set.
- Has(value): checks if the value is in the set.
- Delete(value): deletes an element from set
- Clear(): removes all the elements from the set.
- Size: returns the numbers of elements in a set.
- forEach():
method invokes (calls) a function for each Set element.
CHADSF
Map methods
- Set(key, value): sets the value of a specific key in the map.
- Get(key): Returns the value associated with a specificied key in the map.
- Has(key): returns a boolean indicating whether a Map contains a key.
- Delete(key): removes the related key and value and returns a boolean indicating whether entry was deleted.
- Clear(): removes all the entries from the Map.
- Size: returns the number of entries.
- Entries(): returns a new iterator object that ccontains an array of [key, value] for each entry in the map.
- Keys()
- Values()
Higher Order Function
A higher-order function is a function that takes one or more functions as arguments, or returns a function as its result.