Skip to content

Instantly share code, notes, and snippets.

@injust90
Created April 7, 2020 17:42
Show Gist options
  • Save injust90/55652a6b9b49dc213594ef98ba8be756 to your computer and use it in GitHub Desktop.
Save injust90/55652a6b9b49dc213594ef98ba8be756 to your computer and use it in GitHub Desktop.
Arrow function expressions and classes
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());
@injust90
Copy link
Author

injust90 commented Apr 7, 2020

Because this.getinformation is a "block body" the return statement is necessary

Source:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#Returning_object_literals

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment