Skip to content

Instantly share code, notes, and snippets.

@jsmayo
Created March 18, 2018 14:12
Show Gist options
  • Save jsmayo/14bd15e915878c9f99dd9a31a8ada847 to your computer and use it in GitHub Desktop.
Save jsmayo/14bd15e915878c9f99dd9a31a8ada847 to your computer and use it in GitHub Desktop.
staticMethods.js created by jsmayo - https://repl.it/@jsmayo/staticMethodsjs
count = 0;
class Person {
constructor(firstName, lastName, favoriteColor, favoriteNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.favoriteColor = favoriteColor;
this.favoriteNumber = favoriteNumber;
}
multiplyFavoriteNumber(num) {
return num * this.favoriteNumber;
}
static countIterator() {
count++;
console.log(count);
}
}
// Static methods can be called without the object being created
Person.countIterator();
// Making a new Person Object
var a = new Person("first", "last", "yellow", 16);
// Looking at current properties
console.log(a);
// Call static method
Person.countIterator();
/* Note
Cannot do a.countIterator() and using a.countIterator
will add the property countIterator to the 'a' object. */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment