Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <body>
- <script>
- // 1)
- // function on global scope and "this" will relate to window object.
- // function getName(){
- // return this.name; // because the function is in the global scope "this" will relate to "window" obj;
- // }
- // let name = "gal";
- // let obj = { name:"Chen", getName: getName() };
- // let obj555 = { name:"moshe"}
- // console.log(obj.getName); // be "gal"
- // // call will change the "this" context to our obj "this" and show "chen";
- // let result = getName.call(obj555);
- // console.log(result); // be "chen"
- // console.log(obj.getName ) // chen
- ///
- // 2 )
- let obj2 = {
- name:"Chen",
- getName : function(){
- return this.name; // "this" is the object that contains the function
- }
- };
- // because the word "this" applied to the obj2 and she called from the inner scope of the object.
- let unbound = obj2.getName; // pointer to function, "this" will point to global
- console.log(unbound());
- console.log(`obj2 ${obj2.getName()} ` ) // we called the function from the object and due that "this"
- // // 3 ) // Yariv
- // let getName2 = () => {
- // return this.name3; //
- // }
- // let name3 = "gal";
- // let obj3 = { name3:"Chen", getName: getName2() };
- // console.log(obj3.getName); // chen why? the arrow function knows to relate "this" to the object when we call the function.
- // let camera = {
- // myPrice: () => {
- // console.log("test");
- // return this.price;
- // }
- // }
- // let cameraPrice = { price : 500};
- // console.log(camera.myPrice.call(cameraPrice));
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement