12

I found a great description of the semantic difference between Properties and Methods (paraphrased, via http://www.webdeveloper.com/forum/showthread.php?133712-Properties-Vs.-Methods):

Properties are like nouns. They have a value or state.

Methods are like verbs. They perform actions.

A property can't perform an action and the only value that a method has is the one that is returned after it finishes performing the action.

e.g.

Property: door; Possible Values: open, closed

Method: openDoor; Action: to change the value of the door property to "open"

Creating an example: I understand this in theory but I can't come up with an example. Would it be possible to show me how the door/openDoor would look in actual Javascript code?

4
  • You're probably ahead of yourself; you should read up on Object-Oriented Programming in general first, as your question is not specific to Javascript. If the distinction between data and functions is also unclear to you, you should back up even further.
    – lanzz
    Commented Feb 19, 2013 at 8:41
  • 2
    Also, I think the distinction doesn't really apply to JS in particular, as a method is only a function stored in a property (implicitly getting the function context set to the object upon calling it as a method).
    – Yoshi
    Commented Feb 19, 2013 at 8:44
  • 1
    It doesn't have to apply to JS in particular for me to wonder how it works in the specific language that I'm currently learning. I appreciate the links and the advice, I will look through those. What I'm trying to understand is the semantics of JS, how to write it, that's an important part of the process too. Everyone has their own learning style and this would really help me understand JS.
    – jon
    Commented Feb 19, 2013 at 8:47
  • @jon the point is that different languages define these things very differently and in contradictory ways. So javascript doesn't quite distinguish properties and methods in that way.
    – barlop
    Commented Aug 17, 2015 at 19:03

3 Answers 3

22

Really, you need to back up and read some of the links posted above. But as a quick example:

var house = {} ;

house.isDoorOpen = false ;

house.openDoor = function(){
    house.isDoorOpen = true ;
}

Here house is the object. It has a property: house.isDoorOpen. Here, it is more like an adjective. Either the door is open (true) or closed (false). As it sounds, it describes a property of the house.

Also, it has a method openDoor (which is used like this: house.openDoor() ). That's something that it can do. In this case, the action openDoor affects the isDoorOpen property, making it true.

3
  • what is the difference b/w properties and variables in javascript. All I can think of is as: objects variables are called properties and ouside everything is variable we don't call them properties. Commented Apr 18, 2015 at 5:34
  • 2
    @androidplusios.design We call values that belong to objects "properties". I think this is a logical use of English. "The house has the property of having an open door"
    – ColBeseder
    Commented Apr 19, 2015 at 19:08
  • @ColBeseder You write "We call values that belong to objects "properties"" <-- No! Certainly not. You might mean that variables that belong to objects are called properties. Likewise that identifiers declared as constant, and belonging to objects, are called properties. One of those "names" that has a value.
    – barlop
    Commented Mar 29, 2022 at 21:10
4

Let's look at how the ECMA-262 2023 ECMAScript Standard describes the terms property and method:

4.4.36 property

part of an object that associates a key (either a String value or a Symbol value) and a value

NOTE: Depending upon the form of the property the value may be represented either directly as a data value (a primitive value, an object, or a function object) or indirectly by a pair of accessor functions.

4.4.37 method

function that is the value of a property

NOTE: When a function is called as a method of an object, the object is passed to the function as its this value.

Also, JavaScript's definition of attribute is different from Java's:

4.4.39 attribute

internal value that defines some characteristic of a property


for... in loops through an object's enumerable properties, and that includes its functions.

https://eloquentjavascript.net/06_object.html

In JavaScript, methods are nothing more than properties that hold function values.

There does seem to be a more standard definition of property..

https://en.wikipedia.org/wiki/Property_(programming)

A property, in some object-oriented programming languages, is a special sort of class member, intermediate between a field (or data member) and a method. .... Some object-oriented languages, such as Java and C++, don't support properties, and require the programmer to define a pair of accessor and mutator methods instead.

In that more standard, non-JavaScript definition of property, C# has properties, and Java doesn't have properties.

0

Object in JavaScript is just key-value pairs stored in a Hash. The difference between b/w property and method is that - property is a value stored in the hash key, whereas method is a function stored in the hash key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.