DEV Community

Cover image for From Pizza to Samosas: Understanding Object Properties and Methods in JavaScript
kamlesh-21
kamlesh-21

Posted on

From Pizza to Samosas: Understanding Object Properties and Methods in JavaScript

Have you ever wondered what "object properties" and "methods" mean in JavaScript? These terms might sound confusing at first, but they are actually just ways to describe the characteristics and actions of objects in the language. To help you understand these concepts better, Let's take a look at some everyday examples that you can relate to.

We'll start with a familiar food item: pizza. A pizza can be thought of as an object in JavaScript, with different properties that describe its size, toppings, and crust type. For example, a large pepperoni pizza with a thin crust would have the following properties:

const pizza = {
size: 'large',
toppings: ['pepperoni'],
crustType: 'thin'
};

But a pizza can also have methods that allow you to perform actions on it. For example, you can cut a slice of pizza or eat a slice of pizza. These methods use the properties of the pizza object to perform the action. Here's an example:

const pizza = {
  size: 'large',
  toppings: ['pepperoni'],
  crustType: 'thin',
  slice: function() {
    console.log('You have cut a slice of pizza.');
  },
  eat: function() {
    console.log('You have eaten a slice of pizza.');
  }
};

pizza.slice(); // Output: You have cut a slice of pizza.
pizza.eat(); // Output: You have eaten a slice of pizza.
Enter fullscreen mode Exit fullscreen mode

In this example, the slice() and eat() methods are like actions that you can perform on the pizza object. They use the properties of the pizza, like its size and toppings, to perform the action.

Now let's move on to a different type of food: samosas. A samosa is a popular Indian street food that is shaped like a triangle and filled with spiced potatoes. Like pizza, a samosa can be thought of as an object in JavaScript, with different properties that describe its filling, spiciness, and shape. Here's an example:

const samosa = {
  filling: 'potatoes',
  spiciness: 'medium',
  shape: 'triangular',
  bite: function() {
    console.log('You have bitten into the samosa.');
  },
  dip: function() {
    console.log('You have dipped the samosa in chutney.');
  }
};

samosa.bite(); // Output: You have bitten into the samosa.
samosa.dip(); // Output: You have dipped the samosa in chutney.
Enter fullscreen mode Exit fullscreen mode

In this example, the bite() and dip() methods are like actions that you can perform on the samosa object. They use the properties of the samosa, like its filling and spiciness, to perform the action.

So, as we can see, object properties and methods in JavaScript are just ways to describe the characteristics and actions of objects, using examples that you can relate to in your everyday life.

And remember, when it comes to JavaScript objects and food, the possibilities are endless. Just don't try to eat your code!

Top comments (0)