Advertisement
Guest User

Untitled

a guest
May 27th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. var obj = (function ($) {
  2. return {
  3. func1: function () {
  4. console.log("do something");
  5. },
  6. func2: function () {
  7. // do something
  8. $("#debug").click(function () {
  9. // do something
  10. func1();
  11. // do something
  12. })
  13. }
  14. }
  15. })(jQuery);
  16.  
  17. obj.func2(); // по клику на #debug получаем - Uncaught ReferenceError: func1 is not defined
  18.  
  19. var obj = (function ($) {
  20. return {
  21. func1: function() {
  22. console.log("do something");
  23. },
  24. func2: function () {
  25. $("#debug").click(function () {
  26. this.func1(); //2. обращаемся к нужному контексту
  27. }.bind(this)) //1. привязываем контекст
  28. }
  29. }
  30. })(jQuery);
  31.  
  32. obj.func2();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement