Day 98/100 Objects in Code

riocantre

Rio Cantre

Posted on December 22, 2021

Day 98/100 Objects in Code

banner

It’s worth noting that while we can represent real-world objects as JavaScript objects, the analogy does not always hold. This is a good starting place for thinking about the structure and purpose of objects, but as you continue your career as a developer, you’ll find that JavaScript objects can behave wildly different than real objects.

Object Literals

var sister = {
    name: "Sarah", 
    age: 23,
    parents: [ "alice", "andy" ],
    siblings: ["julia"],
    favoriteColor: "purple",
    pets: true
};
Enter fullscreen mode Exit fullscreen mode

The syntax you see above is called object-literal notation. There are some important things you need to remember when you're structuring an object literal:

  • The "key" (representing a property or method name) and its "value" are separated from each other by a colon

  • The key: value pairs are separated from each other by commas

  • The entire object is wrapped inside curly braces { }.

  • And, kind of like how you can look up a word in the dictionary to find its definition, the key in a key:value pair allows you to look up a piece of information about an object.

Here's are a couple examples of how you can retrieve information about my sister's parents using the object you created.

sister["parents"] // returns [ "alice", "andy" ]
sister.parents // also returns ["alice", "andy"] 
Enter fullscreen mode Exit fullscreen mode

Using sister["parents"] is called bracket notation (because of the brackets!) and using sister.parents is called dot notation (because of the dot!).

  • Objects are one of the most important data strcutures in JavaScript.

  • They have properties (information about the object) and methods (functions or capabilities the object has).

  • Objects are an incredibly powerful data type and you will see them all over the place when working with JavaScript, or any other object-oriented programming language.

Code Snippets

var savingsAccount = {
    balance: 1000,
    interestRatePercent: 1,
    deposit: function addMoney(amount) {
           if (amount > 0) {
               savingsAccount.balance += amount;
           }
    },
    withdraw: function removeMoney(amount) {
           var verifyBalance = savingsAccount.balance - amount;
           if (amount > 0 && verifyBalance >= 0) {
               savingsAccount.balance -= amount;
           }
    },
    printAccountSummary: function() {
    return "Welcome!\nYour balance is currently $" + savingsAccount.balance + " and your interest rate is " + savingsAccount.interestRatePercent + "%.";
    }
};
console.log(savingsAccount.printAccountSummary());



var savingsAccount = {
    balance: 1000,
    interestRatePercent: 1,
    deposit: function addMoney(amount) {
           if (amount > 0) {
              savingsAccount.balance += amount;
           }
    },
    withdraw: function removeMoney(amount) {
           var verifyBalance = savingsAccount.balance - amount;
           if (amount > 0 && verifyBalance >= 0) {
               savingsAccount.balance -= amount;
           }
    }
};
Enter fullscreen mode Exit fullscreen mode

Summary

Woke up today humming a tone... mumbling words then I search about the lyrics and it goes like this...

If you search for tenderness.It isn't hard to find
You can have the love you need to live. But if you look for truthfulness. You might just as well be blind. It always seems to be so hard to give.

Resource

💖 💪 🙅 🚩
riocantre
Rio Cantre

Posted on December 22, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 100/100 Reverse Function
100daysofcode Day 100/100 Reverse Function

December 24, 2021

Day 99/100 Scope
100daysofcode Day 99/100 Scope

December 23, 2021

Day 98/100 Objects in Code
100daysofcode Day 98/100 Objects in Code

December 22, 2021

Day 97/100 Donuts to Code
100daysofcode Day 97/100 Donuts to Code

December 21, 2021

Day 96/100 Data Types
100daysofcode Day 96/100 Data Types

December 20, 2021