Sat, August 31, 2024
Hello, fellow coders! π
Today was a big day in my JavaScript journey as I tackled the biggest assignment in the JavaScript Syntax II lesson: Objects.
Weβve been using methods, like Math.floor() in random number generation, where floor() is the method that returns the lower integer value when rounded down. So, itβs good to see how methods are created. Objects are collections of properties in the form of key-value pairs, such as this one named spaceship, with homePlanet Earth:
const spaceship = {
'Fuel Type': 'Turbo Fuel',
homePlanet: 'Earth',
mission: 'Explore the universe'
};
Object properties can be accessed using either dot notation or bracket notation. You can add a property to an object using assignment or delete one using the delete keyword:
spaceship.color = 'glorious gold';
spaceship.numEngines = 5;
delete spaceship['Secret Mission'];
When an objectβs properties include a function, this is called a method. Unlike standard properties, methods are not what it is, but what it does, such as in the method invade:
spaceship.invade = () => {
console.log('Hello! We have come to conquer your planet. Instead of Earth, it shall be called New Xaculon.');
};
spaceship.retreat = () => {
console.log('We no longer wish to conquer your planet. It is full of dogs, which we do not care for.');
};
Objects are the core of JavaScript, so it's good to get a handle on them!
Top comments (0)