Submodule 12.2: JavaScript Objects
Submodule 12.2: JavaScript Objects
- Objects Introduction
- Access Object Properties
- Changing, Adding and Deleting properties
- Object Methods
- The this keyword
Object Methods
Methods are the actions that can be applied to objects.
When a property of an object is a JavaScript function, we call it method.
Example:
var darkChoco = {
cacao:"80%",
milk:"0%",
grams:100,
getFullInfo : function () {
return this.cacao + " " + this.milk + " " + this.grams;
}
};
In the above example, if we are referring to getFullInfo, we are referring to the function as a value of that property. However, if we write getFullInfo followed by parenthesis, getFullInfo(), then we are refer to the method of that object.
To understand the difference between the two, go to your console and copy the code of the example. Next, do two tests. The first time type in the console darkChoco.getFullInfo;
. The second time type darkChoco.getFullInfo();
What did you got each time?
darkChoco.getFullInfo;
: ƒ () { return this.cacao + " " + this.milk + " " + this.grams; } Result for
darkChoco.getFullInfo();
: "80% 0% 100"In our example, you may have noticed the usage of a new expression, .this. The subject of our next chapter will be to explain this new keyword.