Created
October 1, 2018 18:09
-
-
Save talyssonoc/90a3ff2870bf229092159801a744cb85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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