Get & Set
This lesson teaches us how to use the "get" and "set" keywords in JavaScript.
We'll cover the following...
We'll cover the following...
Using get
Keyword
Let’s revise our knowledge for this
keyword:
Press + to interact
Javascript (babel-node)
var employee = {name: 'Joe',age: 28,designation: 'developer',//function returning designation of the employeedisplay() {return this.designation //using this to refer to the "employee" object}}//this will display the designationconsole.log(employee.display())
Here, the function display()
was being used to get the value of the property designation
. Another way to do this is by using the get
keyword.
Example
Let’s take a look at an example implementing the get
keyword.
Press + to interact
Javascript (babel-node)
var employee = {name: 'Joe',age: 28,designation: 'developer',//function returning designation of the employeeget display() {return this.designation //using this to refer to the "employee" object}}//this will display the designationconsole.log(employee.display)
Explanation
You must be wondering what the difference is ...