Object in JavaScript | How to Create and How to Access Atributes || Lecture 08
Welcome to this JavaScript tutorial on creating and accessing attributes in objects! In this video, we will learn how to use JavaScript to create objects that represent real-world entities, such as a bike, and how to add attributes to these objects to store and retrieve information about them. We will also learn how to access these attributes using dot notation and bracket notation. Creating a Bike Object Let's start by creating a bike object using JavaScript. We can define a bike object as follows: javascript let bike = { brand: "Trek", model: "Fuel EX 9.8", year: 2022, color: "black", price: 5499.99 }; In this code, we define a variable bike and assign it an object literal that contains five attributes: brand, model, year, color, and price. Each attribute has a corresponding value that represents the brand, model, year, color, and price of the bike, respectively. Accessing Attributes with Dot Notation Now that we have created a bike object, we can access its attributes using dot notation. Dot notation is a way to access an object's attributes by specifying the object's name followed by a dot and the attribute's name. For example, to access the brand attribute of the bike object, we can write: javascript console.log(bike.brand); // output: "Trek" This code will print the value of the brand attribute, which is "Trek", to the console. We can also use dot notation to modify an object's attributes. For example, to change the color attribute of the bike object to "red", we can write: javascript bike.color = "red"; console.log(bike.color); // output: "red" This code will change the value of the color attribute to "red" and print it to the console. Accessing Attributes with Bracket Notation In addition to dot notation, we can also access an object's attributes using bracket notation. Bracket notation is a way to access an object's attributes by specifying the object's name followed by square brackets and the attribute's name as a string. For example, to access the model attribute of the bike object using bracket notation, we can write: javascript console.log(bike["model"]); // output: "Fuel EX 9.8" This code will print the value of the model attribute, which is "Fuel EX 9.8", to the console. We can also use bracket notation to add a new attribute to an object. For example, to add a size attribute to the bike object with a value of "medium", we can write: javascript bike["size"] = "medium"; console.log(bike.size); // output: "medium" This code will add a new attribute called size to the bike object and print its value, which is "medium", to the console. Conclusion Congratulations, you have learned how to create and access attributes in JavaScript objects using dot notation and bracket notation! By using objects to represent real-world entities and adding attributes to them, you can store and retrieve information about these entities in a structured and organized way. Keep practicing and exploring the possibilities of JavaScript objects, and you will become a master of object-oriented programming in no time! Thanks for watching! And Follow on social media-- ✅ Twitter : https://twitter.com/CyberSecur35108?t... ✅ Instagram: https://instagram.com/cybersecuretec?... ✅ Facebook Group: https://www.facebook.com/groups/96787... #JavaScript #Objects #ObjectKeys #DotNotation #BracketNotation #Attributes #BikeExample #Tutorial
Download
0 formatsNo download links available.