objects 12.1

PHOTO EMBED

Mon Jun 27 2022 18:51:23 GMT+0000 (Coordinated Universal Time)

Saved by @cruz #javascript

let tortoiseOne = {
    species: "Galapagos Tortoise",
    name: "Pete",
    weight: 919,
    age: 85,
    diet: ["pumpkins", "lettuce", "cabbage"]
};
//In the case of Pete, our zoo's friendly Galapagos Tortoise, the object tortoiseOne has several properties for his species, name, weight, age, and diet. If we wanted to add a method to our object, we might add a function that returns a helpful statement for the general public.
let tortoiseOne = {
    species: "Galapagos Tortoise",
    name: "Pete",
    weight: 919,
    age: 85,
    diet: ["pumpkins", "lettuce", "cabbage"],
    sign: function() {
        return this.name + " is a " + this.species;
    }
 };
//In the example above, on line 8, we see a keyword which is new to us. Programmers use the this keyword when they call an object's property from within the object itself. We could use the object's name instead of this, but this is shorter and easier to read. For example, the method, sign, could have a return statement of tortoiseOne.name + " is a " + tortoiseOne.species". However, that return statement is bulky and will get more difficult to read with more references to the tortoiseOne object.

content_copyCOPY