3 Replies - 1099 Views - Last Post: 20 March 2013 - 12:53 AM

#1 zhenurik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-March 13

Stuck with 'this'

Posted 19 March 2013 - 11:05 PM

I am watching jQuery lessons on https://tutsplus.com...-learn-jquery/. And I am stuck on the lesson Slides and Structure starting 9.20 minute. This is a simplified code from the video:
var contactForm = {
    container: $('#contact'),
    
    show: function(){
         contactForm.container.show();
    },
    
    close: function() {
         $('<span class=close>X</span'>)
             .prepandTo(this); //#contact

    }
};

For some reason 'this' in the function 'close' refers to contactForm.container. How come it refers to the variable if it supposed to refer to object contactForm? O_o
Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Stuck with 'this'

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: Stuck with 'this'

Posted 19 March 2013 - 11:16 PM

well, it wouldn’t make sense (logically) if this referred to the contactForm object. but in the end it depends on how you call the close() method.
Was This Post Helpful? 1
  • +
  • -

#3 zhenurik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-March 13

Re: Stuck with 'this'

Posted 19 March 2013 - 11:39 PM

Dormilich, where you've been 3 days ago???... )) YOU RIGHT!!! I did not pay attention on that! It's called from the function 'show' - contactForm.close.call(container)!..
THANKS A LOOOOOT!!!
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,257
  • Joined: 08-June 10

Re: Stuck with 'this'

Posted 20 March 2013 - 12:53 AM

View Postzhenurik, on 20 March 2013 - 07:39 AM, said:

Dormilich, where you've been 3 days ago???...

I watched when Hillary the Witch goes to the Opera
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1