Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <meta name="description" content="箭頭函式">
  7. <title>箭頭函式</title>
  8. </head>
  9. <body>
  10. <section>綁定this</section>
  11. <script id="jsbin-javascript">
  12. // ori
  13. const obj = {
  14. a: 1
  15. }
  16.  
  17. function func() {
  18. const that = this
  19.  
  20. setTimeout(
  21. function() {
  22. console.log(that)
  23. }, 2000)
  24. }
  25.  
  26. func.call(obj) //Object {a: 1
  27.  
  28. // arrow
  29.  
  30. function func() {
  31. setTimeout(
  32. () => {
  33. console.log(this)
  34. }, 2000)
  35. }
  36.  
  37. // 用bind來回傳部分函式(partial function)的語法
  38. function add(x, y){
  39. return x + y
  40. }
  41.  
  42. function puls1Factory(y){
  43. return add(1,y)
  44. }
  45.  
  46. const puls1Bind = add.bind(undefined, 1)
  47.  
  48. const puls1Arrrow = y => add(1,y)
  49. </script>
  50. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement