/node_modules/babel-plugin-transform-function-bind/README.md

https://bitbucket.org/cefasgarciapereira/apresentacao-backup-aws · Markdown · 120 lines · 81 code · 39 blank · 0 comment · 0 complexity · dc14eb8d8d187d2e80e52d7a3b86c98d MD5 · raw file

  1. # babel-plugin-transform-function-bind
  2. > Compile the new function bind operator `::` to ES5.
  3. ## Detail
  4. ```js
  5. obj::func
  6. // is equivalent to:
  7. func.bind(obj)
  8. obj::func(val)
  9. // is equivalent to:
  10. func.call(obj, val)
  11. ::obj.func(val)
  12. // is equivalent to:
  13. func.call(obj, val)
  14. ```
  15. [Try in REPL](http://babeljs.io/repl/#?evaluate=true&presets=es2015%2Cstage-0&code=obj%3A%3Afunc%3B%0A%0Aobj%3A%3Afunc(val)%3B%0A%0A%3A%3Aobj.func(val)%3B)
  16. ## Example
  17. ### Basic
  18. ```js
  19. const box = {
  20. weight: 2,
  21. getWeight() { return this.weight; },
  22. };
  23. const { getWeight } = box;
  24. console.log(box.getWeight()); // prints '2'
  25. const bigBox = { weight: 10 };
  26. console.log(bigBox::getWeight()); // prints '10'
  27. // Can be chained:
  28. function add(val) { return this + val; }
  29. console.log(bigBox::getWeight()::add(5)); // prints '15'
  30. ```
  31. [Try in REPL](http://babeljs.io/repl/#?evaluate=true&presets=es2015%2Cstage-0&code=const%20box%20%3D%20%7B%0A%20%20weight%3A%202%2C%0A%20%20getWeight()%20%7B%20return%20this.weight%3B%20%7D%2C%0A%7D%3B%0A%0Aconst%20%7B%20getWeight%20%7D%20%3D%20box%3B%0A%0Aconsole.log(box.getWeight())%3B%20%2F%2F%20prints%20'2'%0A%0Aconst%20bigBox%20%3D%20%7B%20weight%3A%2010%20%7D%3B%0Aconsole.log(bigBox%3A%3AgetWeight())%3B%20%2F%2F%20prints%20'10'%0A%2F%2F%20bigBox%3A%3AgetWeight()%20is%20equivalent%20to%20getWeight.call(bigBox)%0A%0A%2F%2F%20Can%20be%20chained%3A%0Afunction%20add(val)%20%7B%20return%20this%20%2B%20val%3B%20%7D%0A%0Aconsole.log(bigBox%3A%3AgetWeight()%3A%3Aadd(5))%3B%20%2F%2F%20prints%20'15')
  32. ### Using with `document.querySelectorAll`
  33. It can be very handy when used with `document.querySelectorAll`:
  34. ```js
  35. const { map, filter } = Array.prototype;
  36. let sslUrls = document.querySelectorAll('a')
  37. ::map(node => node.href)
  38. ::filter(href => href.substring(0, 5) === 'https');
  39. console.log(sslUrls);
  40. ```
  41. [Try in REPL](http://babeljs.io/repl/#?evaluate=true&presets=es2015%2Cstage-0&code=%0Aconst%20%7B%20map%2C%20filter%20%7D%20%3D%20Array.prototype%3B%0A%0Alet%20sslUrls%20%3D%20document.querySelectorAll('a')%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%3Amap(node%20%3D%3E%20node.href)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%3Afilter(href%20%3D%3E%20href.substring(0%2C%205)%20%3D%3D%3D%20'https')%3B%0A%0Aconsole.log(sslUrls)%3B%0A)
  42. `document.querySelectorAll` returns a `NodeList` element which is not a plain array, so you normally can't use the `map` function on it, and have to use it this way: `Array.prototype.map.call(document.querySelectorAll(...), node => { ... })`. The above code using the `::` will work because it is equivalent to:
  43. ```js
  44. const { map, filter } = Array.prototype;
  45. let sslUrls = document.querySelectorAll('a');
  46. sslUrls = map.call(sslUrls, node => node.href);
  47. sslUrls = filter.call(sslUrls, href => href.substring(0, 5) === 'https');
  48. console.log(sslUrls);
  49. ```
  50. ### Auto self binding
  51. When nothing is specified before the `::` operator, the function is bound to its object:
  52. ```js
  53. $('.some-link').on('click', ::view.reset);
  54. // is equivalent to:
  55. $('.some-link').on('click', view.reset.bind(view));
  56. ```
  57. ## Installation
  58. ```sh
  59. npm install --save-dev babel-plugin-transform-function-bind
  60. ```
  61. ## Usage
  62. ### Via `.babelrc` (Recommended)
  63. **.babelrc**
  64. ```json
  65. {
  66. "plugins": ["transform-function-bind"]
  67. }
  68. ```
  69. ### Via CLI
  70. ```sh
  71. babel --plugins transform-function-bind script.js
  72. ```
  73. ### Via Node API
  74. ```javascript
  75. require("babel-core").transform("code", {
  76. plugins: ["transform-function-bind"]
  77. });
  78. ```
  79. ## References
  80. * [Proposal](https://github.com/zenparsing/es-function-bind)
  81. * [Babel Blog: Function Bind Syntax](/blog/2015/05/14/function-bind)