Why in the console writes:

Uncaught TypeError: commercialOfferModal.close is not a function

Why is self or commercialOfferModal undefined in this code?

If commercialOfferModal.close(); call the commercialOfferModal function from the outside, then everything works, but not from the inside. Because commercialOfferModal is undefined ?

 (function commercialOfferModal(){ var self = this; $('.commercial-offer-modal_open').click(function(){ $('.commercial-offer-modal__overlay').fadeIn(300); $('.commercial-offer-modal__content').animate({'right': "0"}, 300); }); $('.commercial-offer-modal_close').click(function(){ self.close(); }); $('.commercial-offer-modal__overlay').click(function(){ self.close(); }); })(); commercialOfferModal.close = function(){ $('.commercial-offer-modal__overlay').fadeOut(200); $('.commercial-offer-modal__content').animate({'right': "-704px"}, 300); $('input').removeClass('error'); $('p.error-text ').text(''); }; 

    1 answer 1

    Change your code with:

      commercialOfferModal.close = function(){ 

    on:

      commercialOfferModal.prototype.close = function(){ 

    The problem is that you add a method to the object, and not to the commercialOfferModal instance. In other words, the method will be available at the full link commercialOfferModal.close and beyond, but not available through this, since it’s another scope

    • And where in the code from a question is creation of copies? - Pavel Mayorov