Javascript objects

¡Supera tus tareas y exámenes ahora con Quizwiz!

what actually mutates objects permenantly instead of just reassigning? and can overcome initial const declaration?

As a result, functions which change object properties actually mutate the object permanently

in this case, why would you use bracket notation instead of dot notation? let returnAnyProp = (objectName, propName) => objectName[propName]; returnAnyProp(spaceship, 'homePlanet'); // Returns 'Earth'

If we tried to write our returnAnyProp() function with dot notation the computer would look for a key of 'propName' on our object and not the value of the propName parameter.

when do we have to use bracket notation when accessing an objects property/value?

We *must* use bracket notation when accessing keys that have numbers, spaces, or special characters in them.

method

When the data stored on an object is a function

in this case, why is fuel type in quotations and color is not? let spaceship = { 'fuel type' = 'diesel, color: 'blue', }

because you can leave out the quotations if there is no special characters, for example, the space inbetween fuel and type

what are key-value pairs separated by?

commas

if you had this, how would you delete the homePlanet from the object? let spaceship = { 'Fuel Type' : 'Turbo Fuel', homePlanet : 'Earth', color: 'silver', }

delete spaceship.homePlanet;

if you had this, how would you delete the fuel type from the object? let spaceship = { 'Fuel Type' : 'Turbo Fuel', homePlanet : 'Earth', color: 'silver', }

delete spaceship['Fuel Type'];

what are the two ways to access an object's property/value?

dot and bracket notation

if you wanted to make a loop using for...in for an object, what is the syntax for the for....in part

for (let variableName in outerElement.InnerElement)

what is for...in?

for in is the looping mechanism for objects, that executes a block of code for each property in an object

in this case, what is the key, and what is the value? let spaceship = { 'fuel type' = 'diesel, color: 'blue', }

fuel type and color are keys, diesel and blue are values

if you had the following code, how would you change the fuel type to avocado oil using a function called greenEnergy? let spaceship = { 'Fuel Type' : 'Turbo Fuel', homePlanet : 'Earth' };

let greenEnergy = obj => { obj['Fuel Type'] = 'avocado oil'; } greenEnergy(spaceship);

what is the basic syntax to assign an empty object to a variable called spaceship?

let spaceship = {};

what are objects?

objects in JavaScript are containers that store data and functionality.

in this case, how would we access the properties(values) of spaceship using dot notiation? let spaceship = { homePlanet: 'Earth', color: 'silver' };

spaceship.homePlanet; spaceship.color;

if you had this, how would you add a new property called style with the value of alien? let spaceship = { 'Fuel Type' : 'Turbo Fuel', homePlanet : 'Earth', color: 'silver', }

spaceship.style = 'alien';

in this casem how would you change the fuel type to vegetable oil and the color to gold?? let spaceship = { 'Fuel Type' : 'Turbo Fuel', homePlanet : 'Earth', color: 'silver', }

spaceship['Fuel Type'] = 'vegetable oil'; spaceship.color = 'gold';

in this case, how would you access the objects fuel type? let spaceship = { 'Fuel Type': 'Turbo Fuel', 'Active Duty': true, homePlanet: 'Earth', numCrew: 5 };

spaceship['Fuel Type'];

what does it mean to say that objects are passed by reference?

when we pass a variable assigned to an object into a function as an argument, the computer interprets the parameter name as pointing to the space in memory holding that object. As a result, functions which change object properties actually mutate the object permanently

what do you do need to do with the variable name calling the variables in for...in

you place it inside square brackets instead of the dot notation when calling the specific variable

what are key value pairs held in?

{}

We fill an object with unordered data. This data is organized into key-value pairs. What is a key value pair?

A key is like a variable name that points to a location in memory that holds a value.

what is the difference between a property and a method?

A property is what an object has, while a method is what an object does.


Conjuntos de estudio relacionados

Health Assessment Exam 3 Questions

View Set

INTRO Exam 2: Cultural competence

View Set

COMPTIA Chapter 3.3 Cases and Motherboards

View Set

MO life and health missed questions

View Set