You could describe an object as a “package” or container used to keep related information and actions organized in one place. Instead of having a bunch of separate, loose notes (variables) and instructions (functions) scattered around, you bundle them together into one named unit. For example, if you were describing a person, the object would hold all their specific details—like their name and age—along side things they can do, like introducing themselves.
An object literal is when you write out the contents of an object directly using curly braces {}. Some key advantages include:
The main difference lies in how you access the information inside them:
While dot notation (e.g., person.age) is generally preferred because it is easier to read, you must use bracket notation when the property name is stored in a variable.
Example: If you have a function that needs to look up a property, but you don’t know which one until the function is actually running, you would use brackets:
const person = { name: "Bob", age: 32 };
let propertyName = "name";
// This works because it looks for the value stored in the variable
console.log(person[propertyName]); // Output: "Bob"
// This would NOT work because it would look for a property literally named "propertyName"
console.log(person.propertyName); // Output: undefined
Bracket notation is also required when you want to set a property name dynamically, such as using a value typed into a text input by a user.
thisthis in JavaScriptthis refer to?Let’s evaluate the following code:
const dog = {
name: 'Spot',
age: 2,
color: 'white with black spots',
humanAge: function (){
console.log(`${this.name} is ${this.age*7} in human years`);
}
}
Inside the humanAge function, this refers to the dog object itself.
When you call dog.humanAge(), JavaScript sets the context of this to whatever is “to the left of the dot.” Because this points to dog, the code interprets the properties as follows:
this.name becomes dog.name (“Spot”)this.age becomes dog.age (2)thisUsing this instead of hardcoding the variable name (like dog.name) provides several key benefits:
dog variable to myPet, the function would still work perfectly. If you had hardcoded dog.name inside the function, the code would break after the rename.cat object with the same function, this.name would correctly point to the cat’s name without you having to rewrite the logic.In the context of an object method, the term this refers to the current object the code is being executed in (or the object the method was called on).
The Advantage of Using this:
The primary advantage is that it allows the same method definition to be reused for multiple objects. Because this points to whatever object is currently “active,” the code can access the specific data for that particular instance without needing to know the object’s name ahead of time. This becomes essential when using constructors to create many different objects from a single template, ensuring each object can correctly refer to its own unique properties.
The Document Object Model (DOM) is a programming interface and an object-oriented representation of web documents, such as HTML, XML, or SVG files. It represents the structure of a document in memory as a logical tree, where each branch ends in a node containing objects. This model allows programs to access and manipulate the page’s structure, style, and content.
The relationship between the DOM and JavaScript can be described through the following key points:
document.querySelector() or document.createElement() are part of the DOM API that JavaScript calls to perform actions.