I think the lesson has been updated since @mikegleeson700029557 has submitted his post
TL;DR, a property is an inherent value in the data, a method is a set of instructions to be carried out by the program
From my understanding, and bear in mind that I am going through the beginning JS tutorial, a property is an inherent value of the data put in JS. Like how all strings have lengths. Methods are a set of instructions that carry out a specific task. For example, the log method prints what is contained within it to the console.
What is the purpose of using parentheses with the property even when they never have any parameters?
Can you give us an example so we have some context?
Iâm assuming that youâre referring to methods (which are actually properties)? Well, methods require parenthesis the same way functions require parenthesis - they donât only need them when they have parameters. When you want to invoke a method, you also need parenthesis as this instructs the interpreter to actually invoke the method and not retrieve a function reference
Great explanation! Thanks
That confuses me, sorry. In the previous example from the lesson we had a string (or so I thought) followed by the length property:
console.log(âhelloâ .length);
So now I am confused about what here is an object and what is a string
And here I am confused again. What is the difference between property and attribute? since you refer to it as both.
Strings, booleans and numbers are primitive types that are given a wrapper object by JS. Most everything in JS is an object.
The terms are interchangeable. In JS âpropertyâ is the more commonly used term.
Thank you so much
Now I understand it!
Generally speaking,
- a property is something that âbelongsâ to the object
For example, if we have an object called donaldTrump
, we can refer to his age by donaldTrump.age
(here age
is a property - no function is called).
- a method relates to an âactionâ upon the object
We can also do donaldTrump.speak()
, which returns his favourite quote as the output of a function (or action) .speak()
. Note that we can pass arguments to methods, but not to properties. If we want his favourite quote in Italian, we may do donaldTrump.speak('italian')
.
Hope it helps.
Property is a characteristic of an Object. Whereas Method is an action that an Object can do. For instance, In English grammar you have a NOUN and a VERB, NOUN is like property and VERB is like a Method.
In regards of methods, there are many possibilities that this action can be done. the method can just converter all letters into CAPITAL, but what about if you want to convert only the First letter, or the first 3 letters?, then there must a way to indicate your preference and this is done by opening brackets â(â and closing brackets")", thus within this area you can input a number to indicate how many letters need to be converted to CAPITAL ones. You will also require something else, like size, isnât it, the the method need to be able to change size. Again you can indicate the size adding a number within the brackets.
Here it comes the trouble, if you say .toConvertCapital(3,5), where you want 3 first letters and 5 as size font, how JAVASCRIPT understand this? why not 5 first letters and 3 font size? the answer is , there is already a specific location of these numbers are their meaning for the method, they were defined when the method was created. So before using a method , you need to read its documentation. If a method do not require and input number, then it will be â()â to keep the same pattern along JAVASCRIPT.
I think this might be rightâŚA property is just an observation of the value of the string (or whatever). The method is actually changing the string. Cat has 3 letters. (property). Change cat to CAT. (method) No?
That is easier to understand. Thanks!
Think of property as a noun, and method as a verb. A method is what is used to change the state of the property.
e.g Property = window: the possible value is open or close, so open and close are properties of the window.
Method = closeWindow: this calls on the action to change the property of the window to close.
Hi, may i know why the lectures or exercise dosenât menthion the use of () in front of the properly or method?
I think it is essential to mention it in the lesson.
For example where it says console.log(âxxxxxxâ.toUpperCase)?, the example should mention or state somewhere that we should ensure to add the parenthesis to the method like so console.log(âxxxxxxâ.toUpperCase()).
At first i was abit lost and confused as to why i could not pass the exercise then i went back to check and i realized i needed to add ().
This was a really concise answer. Much appreciated!
Thanks for a very clear explanation!
So basically, you can access BOTH properties, if any, AND methods, if any, of an object or function just with the Dot Operator? The difference is, you are calling a property if it does not have parentheses, in the case of .lengthâŚ
On the other hand, a method has the parentheses, always⌠In the case of .floor() âŚ
Any that exist, yes. We can also set a new property the same way:
obj.newprop = value
Methods are functions so need to be invoked, hence the parens.
Thank you so much for this easy to understand explainantion!