+ 1
How do you differentiate a method and a variable of an object?
They are named the same but method is just put as a function outside of the object.
3 Réponses
+ 2
A method, a function within an object, will have parenthesis after its name e.g.
function methodName () { //code }
A property, a variable within an object, will not have parenthesis after its name e.g.
var propertyName;
A method (function) or a property (variable) belonging to an object will be called based on the object they belong to:
objectName.methodName ();
objectName.propertyName;
+ 1
I looked it up and was led to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof. (Note that methods are "functions".) So if you ever need your code to do the "differentiating" you ask for, call on
-->typeof object.someMysteriousProperty<--
and wherever you put that, you'll return "function" if it's a method, or "number"/"string"/"object"/etc. if it's not.
- 2
a variable holds a data type