Difference between a property and a method?

If you simply put it, property is a value and method is an action.

The property is simply an object’s variable to hold some data while the method is an object’s function that may or may not take an argument, do some operations, and returns some value.

For example, a console is an object having a log as its function that takes whatever value is being entered within the parenthesis and returns’ some value.
length is a variable of String that internally calculates the length of a given string and assigns the value to the length variable.

The easiest way to think of this is:

  • A property is some data about an object. It defines what it is, such as your age being 20

  • A method is an ‘action’ that an object can take. It defines what it does, such as you being able to walk

*Note: there are special cases and this isn’t always 100% correct (objects might not always ‘take an action’ with a method per se), but this is a simple way of thinking of it


[quote=“mtf, post:4, topic:489753”]
plain object

What makes an Object plain

Any object literal that has no methods, only key/value pairs would qualify as a plain object. This is a generalized term, not a scientific definition.

obj = {
    item: "keyboard",
    model: "gaming",
    number: "1089"

Of course, an object literal with methods will also have no length.

I like your explanation a lot, @mtf !

I also noticed you wrote “Methods do not modify code.” So, do properties modify code? And can some properties retrieve data as well—for instance, like how .length logs the character count of a string? :eyes:

1 Like

Probably stretching it a bit there. The program is running so the only things we can do are, use control flow to select a method or function, create functions with a factory, or morph an object’s prototype to add behavior. It goes above this discussion and doesn’t come up until later. For now it is enough to know that methods allow us to access data properties and read or write to them.

Thank you!

I think I get the idea… it’s just, wow. All along I pretty much thought that methods were the “big idea” but it turns out that methods are actually all properties :exploding_head: So I guess that means properties don’t retrieve data, since some properties are the data? Anyway, this does help!

1 Like

This slide name Properties doesn’t mention methods explicitly, I found the cheat sheet as a good place to get my bearings. After reading a couple of post that seemed to be over my head I was at least able to get a working definition from the cheat sheet

In OOP (Object Oriented Programming), an object has properties and methods. An object is just a blueprint. Take Toyota Camry as an example for an object. Each instance of this car is produced according to a prototype. That prototype will explain what actions(method) the car can perform and what qualities(property) does the car posses. Attributes like the color, the model, doors, wheel, tire and so on. These are all properties. On the other hand the methods are things you can do with Toyota Camry, like accelerate, break, open the door, close the door and any other function that it implements based the designed prototype.
To sum up, methods are things that each object performs hypothetically, in other words they are all the actions that an object does, properties are the object’s attribute and shows its behavior.