Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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