Created
April 7, 2020 17:42
-
-
Save injust90/55652a6b9b49dc213594ef98ba8be756 to your computer and use it in GitHub Desktop.
Arrow function expressions and classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Fruit (type) { | |
this.type = type; | |
this.color = 'unknown'; | |
this.getInformation = function() { | |
return 'This ' + this.type + ' is ' + this.color + '.'; | |
} | |
} | |
function Fruit2(type) { | |
this.type = type; | |
this.color = 'unknown'; | |
this.getInformation = () => {return 'This' + this.type + ' is ' + this.color + '.'}; | |
} | |
let lime = new Fruit('Mexican lime'); | |
console.log(lime.getInformation()); | |
lime.color = 'green'; | |
console.log(lime.getInformation()); | |
let lime2 = new Fruit2('Mexican lime2'); | |
console.log(lime2.getInformation()); | |
lime2.color = 'green2'; | |
console.log(lime2.getInformation()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because this.getinformation is a "block body" the
return
statement is necessarySource:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Returning_object_literals