JS Flashcards
Arrow function?
A concise way to write function expressions, good for simple functions that you use only once (parameters) => some code. IN ARROW FUNCTION WE DON’T NECESSARILY NEED A ‘return’ STATEMENT IF WE HAVE ONLY ONE LINE OF CODE
WHAT IS Objects in JavaScript?
- What is an Object?
An object is a collection of related properties (data) and methods (functions) that describe or represent something.OBJECT = A Collection of related properties and/ or methods
Properties are what an object has
Methods are function that an object can perform
Properties are KEY AND VALUE PAIRS
METHODS ARE FUNCTIONS THAT BELONG TO THE OBJECT
Can represent real world objects(people, products, places)
object = {key : value,
key: function(){
}💥Properties = What an object has (e.g.,name
,age
)
💥Methods = What an object can do (e.g.,sayHello()
,walk()
)
Example: Aperson
Objectconst person = {// PROPERTIES (data)firstName: “Patrick”,age: 30,isHappy: true,// METHOD (function)sayHello: function() {console.log("Hello!");
}
};2. Accessing Properties & Methods💥Dot Notation (Most Common) console.log(person.firstName); // “Patrick”
person.sayHello(); // Calls the method → “Hello!”💥Bracket Notation (Useful for dynamic keys) console.log(person[“age”]); // 303. Real-World Example (SpongeBob Characters)//Let’s create a SpongeBob character object:
const spongebob = {
firstName: "SpongeBob", lastName: "SquarePants", age: 22, job: "Fry Cook", home: "Bikini Bottom", // Method (action the object can do) laugh: function() { console.log("HAHAHAHAHA!"); }
};
// Accessing properties
console.log(spongebob.firstName); // “SpongeBob”
// Calling a method
spongebob.laugh(); // “HAHAHAHAHA!”4. Why Use Objects?📚Organize related data & functions together (e.g., a user
object with name
, email
, and login()
).
📚Model real-world things (e.g., a car
with color
, speed
, and drive()
).
📚Avoid messy, separate variables (group them logically inside an object).5. Key Takeaways✅ Properties = Data (firstName
, age
)
✅ Methods = Functions (sayHello()
, laugh()
)
✅ Objects group related things for cleaner code.
WHAT IS Constructors in JavaScript?
A constructor is a special function that creates and initializes an object instance of a class or an object type. It’s like a blueprint for creating multiple similar objects efficiently.1. Why Use Constructors?Instead of manually creating each object like this:const person1 = { name: “Alice”, age: 25 };
const person2 = { name: “Bob”, age: 30 };
const person3 = { name: “Charlie”, age: 35 };We can use a constructor function to automate the process:function Person(name, age){
this.name = name;
this.age = age; }
const person1 = new Person(“Alice”, 25);
const person2 = new Person(“Bob”, 30);
const person3 = new Person(“Charlie”, 35);Benefits:✔ Less repetitive code✔ Easier to manage multiple objects✔ Follows the DRY (Don’t Repeat Yourself) principle
Static keyword
Static keyword that defines properties or methods that belong to A class itself, rather than objects created from that.
(class owns anything static, not the objects)
If you want access static properties access it by its class name
WHAT IS GETTER AND SETTER
In JavaScript, getters and setters are special methods that allow us to define how object properties are accessed and modified. 1. What is a Getter? A getter is a method that retrieves the value of a property. It allows us to access a property dynamically without directly exposing the internal value. 2. What is a Setter? A setter is a method that updates the value of a property. It allows us to add validation or transformations before assigning a new value. When and How Do You Use It? Example 1: Basic Getter and Setter class Person {
constructor(name, age) {
this._name = name; // Underscore to indicate private property
this._age = age;
}
// Getter method
get name() {
return this._name;
}
// Setter method
set name(newName) {
if (newName.length > 2) {
this._name = newName;
} else {
console.log(“Name must be at least 3 characters long.”);
}
}
}
const person = new Person(“John”, 25);
console.log(person.name); // Calls getter → “John”
person.name = “Al”; // Calls setter → “Name must be at least 3 characters long.”
person.name = “Alex”; // Updates name
console.log(person.name); // “Alex”
Example 2: Using Getters and Setters in Cypress (Test Automation)
In test automation, getters and setters can be used for handling configurations dynamically.class Config {
constructor() {
this._baseUrl = “https://example.com”;
}
get baseUrl() {
return this._baseUrl;
}
set baseUrl(newUrl) {
if (newUrl.startsWith(“https://”)) {
this._baseUrl = newUrl;
} else {
throw new Error(“Invalid URL format”);
}
}
}
const config = new Config();
cy.visit(config.baseUrl); // Uses getter to get the URL
config.baseUrl = “https://newsite.com”; // Updates base URL safely
cy.visit(config.baseUrl);
Why Use Getters and Setters? Encapsulation: Protects internal data from direct modification.Validation: Allows control over how data is set.Computed Properties: Can return derived values dynamically.Better Debugging & Maintainability.