Advertisement
Guest User

Promise.html - example

a guest
Oct 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <body>
  5.  
  6. <script>
  7.  
  8. // 1)
  9. // function on global scope and "this" will relate to window object.
  10. // function getName(){
  11. // return this.name; // because the function is in the global scope "this" will relate to "window" obj;
  12. // }
  13.  
  14. // let name = "gal";
  15. // let obj = { name:"Chen", getName: getName() };
  16. // let obj555 = { name:"moshe"}
  17.  
  18. // console.log(obj.getName); // be "gal"
  19.  
  20. // // call will change the "this" context to our obj "this" and show "chen";
  21. // let result = getName.call(obj555);
  22. // console.log(result); // be "chen"
  23.  
  24. // console.log(obj.getName ) // chen
  25.  
  26.  
  27.  
  28. ///
  29.  
  30.  
  31. // 2 )
  32. let obj2 = {
  33.  
  34. name:"Chen",
  35. getName : function(){
  36. return this.name; // "this" is the object that contains the function
  37. }
  38.  
  39. };
  40.  
  41. // because the word "this" applied to the obj2 and she called from the inner scope of the object.
  42.  
  43. let unbound = obj2.getName; // pointer to function, "this" will point to global
  44.  
  45. console.log(unbound());
  46. console.log(`obj2 ${obj2.getName()} ` ) // we called the function from the object and due that "this"
  47.  
  48. // // 3 ) // Yariv
  49.  
  50.  
  51. // let getName2 = () => {
  52. // return this.name3; //
  53. // }
  54.  
  55.  
  56. // let name3 = "gal";
  57. // let obj3 = { name3:"Chen", getName: getName2() };
  58.  
  59. // console.log(obj3.getName); // chen why? the arrow function knows to relate "this" to the object when we call the function.
  60.  
  61.  
  62. // let camera = {
  63.  
  64. // myPrice: () => {
  65. // console.log("test");
  66. // return this.price;
  67. // }
  68.  
  69. // }
  70.  
  71. // let cameraPrice = { price : 500};
  72.  
  73. // console.log(camera.myPrice.call(cameraPrice));
  74.  
  75.  
  76. </script>
  77.  
  78. </body>
  79. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement