Skip to content

Instantly share code, notes, and snippets.

@talyssonoc
Created October 1, 2018 18:09
Show Gist options
  • Save talyssonoc/90a3ff2870bf229092159801a744cb85 to your computer and use it in GitHub Desktop.
Save talyssonoc/90a3ff2870bf229092159801a744cb85 to your computer and use it in GitHub Desktop.
// User.js
const LEGAL_AGE = 21;
export const isMajor = (user) => {
return user.age >= LEGAL_AGE;
};
// usage
import * as User from './User.js';
const user = { age: 21 };
User.isMajor(user); // true
// no problem if it's spread
const user2 = { ...user, age: 20 };
User.isMajor(user2); // false
// User.js
export default class User {
static LEGAL_AGE = 21;
constructor({ age }) {
this.age = age;
}
isMajor() {
return this.age >= User.LEGAL_AGE;
}
}
// usage
import User from './User.js';
const user = new User({ age: 21 });
user.isMajor(); // true
// if spread, loses the reference for the class
const user2 = { ...user, age: 20 };
user2.isMajor(); // Error: user2.isMajor is not a function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment