0
0
Javascriptprogramming~15 mins

this in objects in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding <code>this</code> in Objects
📖 Scenario: Imagine you are creating a simple app to manage a few cars. Each car has a brand and a model. You want to create objects for each car and use this inside the object to refer to its own brand and model.
🎯 Goal: You will create a car object with properties and a method that uses this to show the car's full name.
📋 What You'll Learn
Create an object called car with properties brand and model.
Add a method called getFullName inside the car object that returns the full name using this.brand and this.model.
Call the getFullName method and print the result.
💡 Why This Matters
🌍 Real World
Using <code>this</code> in objects helps you write code that works with data inside the same object, like managing details of cars, users, or products.
💼 Career
Understanding <code>this</code> is essential for JavaScript developers because it is used in many real-world applications, including web apps and frameworks.
Progress0 / 4 steps
1
Create the car object with properties
Create an object called car with the properties brand set to "Toyota" and model set to "Corolla".
Javascript
Need a hint?

Use const car = { brand: "Toyota", model: "Corolla" }; to create the object.

2
Add the getFullName method using this
Add a method called getFullName inside the car object that returns a string combining this.brand and this.model separated by a space.
Javascript
Need a hint?

Define getFullName() { return this.brand + " " + this.model; } inside the object.

3
Call the getFullName method
Call the getFullName method on the car object and store the result in a variable called fullName.
Javascript
Need a hint?

Use const fullName = car.getFullName(); to call the method.

4
Print the full name of the car
Print the value of the variable fullName using console.log.
Javascript
Need a hint?

Use console.log(fullName); to show the full name in the console.