在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:learn-co-students/javascript-objects-bootcamp-prep-000开源软件地址:https://github.com/learn-co-students/javascript-objects-bootcamp-prep-000开源编程语言:JavaScript 100.0%开源软件介绍:JavaScript ObjectsOverviewIn this lesson, we'll introduce, define, and work with objects. Objectives
IntroductionWhen we run across a word that we don't know, we often consult a dictionary. A dictionary is, at its core, a list of words; below each word is a definition or set of definitions. If we know the word that we're looking for, we can just look it up in the dictionary and get all its information. To offer another example, imagine a planner. The planner has a list of dates, and each date has a list of times; at each time, there's an event (or not). The planner gives us a way of associating what's happening with the time when it happens. If we look up a given time, we will see what (if anything) is happening then. In programming, structures like dictionaries are called "associative data structures": they contain pairs of keys (words in our dictionary analogy) and values (definitions in our dictionary analogy). In JavaScript, the barebones associative data structure is called an object. That means that in an object, you can look something up by its key and get back its value — just like in a dictionary. In fact, you might hear some people refer to objects as "dictionaries." We're going to call them "objects" because they're instances of JavaScript's capital-O Creating ObjectsYou can create an object in two different ways, with the literal syntax and with the Literal Syntax: var meals = {}; The curly braces ( Object Constructor: var meals = new Object(); You just created another object! You can also initialize an object with key-value pairs when you create it: var meals = { breakfast: "oatmeal" };
// or, equivalently
var meals = new Object({ breakfast: 'oatmeal' }) In this case, Note that all keys in JavaScript objects are strings. This means that even though you can create an object Note also that keys must be unique. If you were to initialize the following object var meals = {
breakfast: 'eggs',
breakfast: 'bacon'
} And then check on the value of { breakfast: 'bacon' } Only the last key-value pair to use var meals = {
breakfast: 'avocado',
lunch: 'avocado',
dinner: 'avocado'
} We might want to consider diversifying our diet, but otherwise the above object works as expected. Similarly, if you have a variable Top Tip: ES 6 provides a way to use variables as object keys — you have to wrap the key in square brackets ( We can access the values in an object using dot notation meals.breakfast // 'oatmeal' or square-bracket notation meals['breakfast'] // 'oatmeal' Note that when we use dot syntax, we do not wrap the key in quotes, and the key must be able to be treated as a string. Square-bracket syntax requires quotes if we're referencing the key directly, but it also gives us additional flexibility — we could also do meals[firstMeal] // 'oatmeal' using the meals.firstMeal //undefined When we use dot notation the key is always taken as the literal string provided. We must use bracket notation if we want to access (or delete) values that belong to a variable key. Adding to an ObjectIt's great that we can initialize an object with some key-value pairs var meals = {
breakfast: 'oatmeal',
lunch: 'burrito',
dinner: 'steak'
} but we might not always know what keys and values we're going to have ahead of time. Luckily, we can add new key-value pairs to objects. If we know the name of the key and its value, we can use the dot syntax to add the new pair: meals.snack = 'yogurt'; See that dot meals.snack // 'yogurt'
meals['snack'] // 'yogurt'
meals.lunch // 'burrito' We can also add key-value pairs using bracket notation: meals['second breakfast'] = 'bagel' This comes in handy, as in the above example, when our key is not a simple string. We can also use variables as keys this way: var sweetMeal = 'dessert'
meals[sweetMeal] = 'cake';
meals.dessert // 'cake'
meals[sweetMeal] // 'cake'
Lest it seem like we can only add new things, we can update existing key-value pairs by using the key: meals.breakfast = 'cereal' Note that all of the changes highlighted above are destructive. This means that if we apply these changes to an object by passing the object to a function, the original object will change. Let's try it out: function destructivelyUpdateObjectWithKeyAndValue(obj, key, value) {
obj[key] = value
return obj
}
const recipe = { eggs: 3 }
destructivelyUpdateObjectWithKeyAndValue(recipe, 'flour', '3 cups')
// returns { eggs: 3, flour: '3 cups' }
// but also:
recipe // { eggs: 3, flour: '3 cups' } Hm, but what if that's not what we wanted to do? What if we wanted to create a new object that stores both the old and the new properties?
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论