Skip to content

Instantly share code, notes, and snippets.

@ricardocanelas
Created June 8, 2020 22:19
Show Gist options
  • Save ricardocanelas/4f55ce7ced7078929b814e857f33b185 to your computer and use it in GitHub Desktop.
Save ricardocanelas/4f55ce7ced7078929b814e857f33b185 to your computer and use it in GitHub Desktop.
javascript-class-method

Class

class Star {
  constructor(name) {
    this.name = name;
  }
  getMessage(message) {
    return this.name + message;
  }
}

const sun = new Star('Sun');
sun.getMessage(' is shining') // => 'Sun is shining'

Shorthand method definition

const collection = {
  items: [],
  add(...items) {
    this.items.push(...items);
  },
  get(index) {
    return this.items[index];
  }
};
collection.add('C', 'Java', 'PHP');
collection.get(1) // => 'Java'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment