Guest User

Untitled

a guest
Jul 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. // First we are creating or getting some users and placing them in `users`
  2. const users = [
  3. { name: 'Bob' },
  4. { name: 'Alice' },
  5. ]
  6.  
  7. // Then we are creating a config object that we could pass around as needed
  8. // Notice we are using shorthand `users` instead of `users: users`
  9. const config = {
  10. appOwner: 'Prakash Pawar',
  11. users,
  12. }
  13.  
  14. // Then we are creating an object that holds functions to quickly access anywhere
  15. const actions = {
  16. showUser: user => console.log('User:', user.name),
  17. addLineThenShowUsers: (users) => {
  18. console.log('\n=================')
  19. users.forEach(user => console.log('User:', user.name))
  20. },
  21. }
  22.  
  23. // Lets investigate what we have now, some Plain Old JavaScript Objects (POJOs)
  24. console.log('We got users:', users)
  25. console.log('We got config:', config)
  26. console.log('We got actions:', actions)
  27.  
  28. console.log('\nWho is the 2nd user?', users[1].name)
  29.  
  30. // We can always check if something is there or not with a ternary operator
  31. // It's like an if/else statement but can be thrown around as a Boolean
  32. // We use names like `isUserLoaded` or `hasBorder` to tell the developer how to read it
  33. const isAlice = (users[1].name === 'Alice') ? true : false
  34. console.log('Is it Alice?', isAlice)
  35.  
  36. // What if we do something crazy now?
  37. console.log('\n')
  38.  
  39. // Let's call the actions we made
  40. actions.showUser(users[1])
  41. actions.addLineThenShowUsers(users)
  42.  
  43. // Lets make an app now
  44. const app = {
  45. users,
  46. config,
  47. actions
  48. }
  49.  
  50. console.log('\nAPP:', app)
  51. console.log('\nApp Owner:', app.config.appOwner)
  52.  
  53. console.log('\n=================')
  54. app.users.forEach((user, i) => {
  55. console.log('User:', user.name)
  56. console.log('Owned by:', app.config.appOwner)
  57. console.log('--------------')
  58. })
  59. console.log('=================')
Add Comment
Please, Sign In to add comment