Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPavle Lucic
10,801 PointsDifference between object methods and object properies?
What is the difference between object METHODS and object PROPERTIES?
4 Answers
William Li
Courses Plus Student 26,868 PointsIn the nutshell, Object in JavaScript is just key-value pairs stored in a Hash. The difference between property and method is that -- property is a value stored in the hash key, whereas method is a function stored in hash key.
var person = {
name: "John Doe",
sayHello: function() {
console.log("hello");
}
}
In this code sample.
-
name
is the property of object person, it stored String "John Doe", you can access it via dot notationperson.name
. -
sayHello
is the method of object, and it is a function. You can also access it using dot notationperson.sayHello()
. Notice thatsayHello
is just a function, and function invocation requires you to use () here.
Dave McFarland
Treehouse TeacherYou can think of object properties like variables of the object, and methods like functions.
For example, a string has a .length
property -- it's like a variable in that it holds a value, the number of letters in the string.
But the .toLowerCase()
method is like a function, because it does something -- it takes the letters in a string and returns all of those letters in lower case.
Pavle Lucic
10,801 PointsHey Dave McFarland ,
Tnx on answer.
Is it length also function (variable that holds function). It takes the value from variable, counts the num of characters and returns it like sum of characters..?
I am little confused.
Dave McFarland
Treehouse TeacherThe .length
property just stores the number of characters in the string. In that way, it's like a variable.
Colin Marshall
32,861 PointsFrom the Mozilla Developer Network:
An object is a collection of properties, and a property is an association between a name and a value. A property's value can be a function, in which case the property is known as a method.
ammonquackenbush
3,867 PointsThink of an object as an object in real life (such as a bicycle). A method is something you can do with the bike (pedal), or something you can do to the bike (re-inflate tire). A property is something about the bike: how many wheels it has (wheels) or its height (height).
Object: Bicycle Properties:
- wheels
- height
- price
- radius of wheel Methods:
- repair
- pedal
Hope this helps.
Pavle Lucic
10,801 PointsPavle Lucic
10,801 PointsThis is a great explanation.
object.something; - it is object with propery; object.something(); is object method;
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 PointsYes, that's correct. :)
Pavle Lucic
10,801 PointsPavle Lucic
10,801 PointsI didnt think about that difference until i watched now video. Sometimes when i read documentation for implementing some new complex js plugin, if found dificult to understand in some parts when there is a mention about methods and function in the same time :)
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 Pointsyeah, method and property are among the fancy terms you often heard when doing Object-oriented programming in JavaScript, and they can be confused at first. But really, a method is nothing more than just a function stored in a Object; whenever you see code like this
object.something()
, you know you are calling a method of the object, or to put it another way, calling a function predefined in the object.xenon thong
3,952 Pointsxenon thong
3,952 PointsHi William,
Sorry for asking, but what is Hash?
William Li
Courses Plus Student 26,868 PointsWilliam Li
Courses Plus Student 26,868 Pointshi, xenon.
Sometimes a Hash may refer to as Hash Table, dictionary, or associative array, they're all the same.
In the nutshell, a Hash is an essential data structure for storing key-value pairs, thereby enables you to retrieve the corresponding value from its key.
For more info on Hash, check out this article. cheers.