Guest User

Untitled

a guest
Feb 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. /*
  2.  
  3. To run, save as demo.js in an empty folder and then run:
  4.  
  5. yarn add babel-cli babel-plugin-transform-class-properties
  6. ./node_modules/.bin/babel-node --plugins transform-class-properties demo.js
  7.  
  8. */
  9.  
  10.  
  11. class Foo {
  12.  
  13. constructor () {
  14. this.name = 'OK'
  15. this.getName5 = this.getName1.bind(this)
  16. }
  17.  
  18. getName1 () { return this.name }
  19.  
  20. getName2 = () => { return this.name }
  21.  
  22. getName3 = function () { return this.name }
  23.  
  24. getName4 = function () { return this.name }.bind(this)
  25.  
  26. }
  27.  
  28.  
  29. class Bar {
  30.  
  31. constructor (foo) {
  32. this.name = 'BAAAAAAAAAAAD'
  33. this.getName1 = foo.getName1
  34. this.getName2 = foo.getName2
  35. this.getName3 = foo.getName3
  36. this.getName4 = foo.getName4
  37. this.getName5 = foo.getName5
  38. }
  39.  
  40. }
  41.  
  42.  
  43. function getName6 () { return this.name }
  44.  
  45. const getName7 = () => { return this.name }
  46.  
  47.  
  48. const foo = new Foo()
  49. const bar = new Bar(foo)
  50.  
  51. bar.getName6 = getName6
  52. bar.getName7 = getName7
  53. bar.getName8 = getName6.bind(foo)
  54. bar.getName9 = getName7.bind(foo)
  55.  
  56. console.log('getName1:', bar.getName1())
  57. console.log('getName2:', bar.getName2())
  58. console.log('getName3:', bar.getName3())
  59. console.log('getName4:', bar.getName4())
  60. console.log('getName5:', bar.getName5())
  61. console.log('getName6:', bar.getName6())
  62. console.log('getName7:', bar.getName7())
  63. console.log('getName8:', bar.getName8())
  64. console.log('getName9:', bar.getName9())
Add Comment
Please, Sign In to add comment