从另一个函数读取函数

时间:2022-12-28 16:43:32

The problem that when I click on .test it does not execute the do_alert(); function and gives me a error:

当我点击.test时它没有执行do_alert()的问题;功能,并给我一个错误:

do_alert(); is not defined.

do_alert();没有定义。

What's the problem? the main function helpers is already read when the page is loaded why can' get this function from logout_users function?

有什么问题?在加载页面时已经读取了主函数助手为什么可以从logout_users函数中获取此函数?

var setup_system = (function($) {
  "use strict";

  return {
    init: function() {
      this.logout_users();
      this.helpers();
    },
    logout_users: function() {
      $(document).on('click', '.test', function(e) {
        e.preventDefault();
        do_alert();
      });
    },
    helpers: function() {
      function do_alert() {
        alert();
      }
    }
  };
})(jQuery);

jQuery(document).ready(function() {
  setup_system.init();
});

NOTE: I try to re-read the helpers function by adding this.helpers() inside logout_users function but nothing change.

注意:我尝试通过在logout_users函数中添加this.helpers()重新读取帮助程序函数,但没有任何更改。

3 个解决方案

#1


3  

It's because you've defined do_alert() within the scope of the helpers function.

这是因为你已经在helpers函数的范围内定义了do_alert()。

To fix this you will need to move that function to within scope of the object you return. You could either put it at root level of the object (which would work fine, but could get messy if you have a lot of 'helper' functions) or you could nest it within your helpers property if you define that as another object. Personally, I'd use the latter to have some semblance of organisation. Try this:

要解决此问题,您需要将该函数移动到返回的对象的范围内。您可以将它放在对象的根级别(这可以正常工作,但如果您有很多'帮助'函数可能会变得混乱),或者如果您将其定义为另一个对象,则可以将其嵌套在helpers属性中。就个人而言,我会用后者来表达一些组织。尝试这个:

var setup_system = (function($) {
  "use strict";

  return {
    init: function() {
      this.logout_users();
    },
    logout_users: function() {
      var _obj = this;
      $(document).on('click', '.test', function(e) {
        e.preventDefault();
        _obj.helpers.do_alert();
      });
    },
    helpers: {
      do_alert: function() {
        alert('foo');
      }
    }
  };
})(jQuery);

jQuery(function() {
  setup_system.init();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">Click me</div>

Note that I cached the reference to the object as _obj outside of the click handler, as within that block this will refer to the clicked .test element.

请注意,我将对象的引用缓存为单击处理程序之外的_obj,因为在该块中,这将引用单击的.test元素。

#2


1  

Do_alert function exist only in helpers method, so you can't access to it.

Do_alert函数仅存在于helpers方法中,因此您无法访问它。

You need to declare your function directly in the logout_user method or outside, try this :

您需要直接在logout_user方法或外部声明您的函数,请尝试以下方法:

 var setup_system = (function ($) {
 "use strict";

 return {

     init: function () {
        this.logout_users();
        this.helpers();
     },

    logout_users: function() {

            function do_alert(){
            alert();
        }

        $(document).on('click', '.test', function(e){
            e.preventDefault();
            do_alert();
        });
    },

    helpers: function () {

        function do_alert(){
            alert();
        }
       }

   };
})(jQuery);

jQuery(document).ready(function () {
    setup_system.init();
});

#3


0  

When helpers is invoked by the initfunction, all that is happening is that do_alert is being declared. But function declarations are hoisted to the top of their lexical scope. The lexical scope of do_alert is the scope defined by the helpers function. Therefore, do_alert is not accessible outside of helpers function.

当initfunction调用helper时,所有发生的事情都是声明do_alert。但是函数声明被提升到它们的词法范围的顶部。 do_alert的词法范围是助手函数定义的范围。因此,在辅助函数之外无法访问do_alert。

A couple things you could do. The first one that comes to mind is: you could have the helpers method define a method called do_alert on the object being returned rather than merely declaring a function, like so:

你可以做几件事。首先想到的是:你可以让helpers方法在被返回的对象上定义一个名为do_alert的方法,而不仅仅是声明一个函数,如下所示:

   helpers: function() {
    this.doAlert = function() {
        alert();
    }
}

When your doAlert() is invoked by the event handler passed to jQuery, it will not work with the above solution. Instead you will need to make sure you call that doAlert on the returned object in that event handler. This is how you can do that:

当你的doAlert()被传递给jQuery的事件处理程序调用时,它将无法使用上述解决方案。相反,您需要确保在该事件处理程序中的返回对象上调用该doAlert。这是你如何做到这一点:

 logout_users: function() {
    var self = this;
  $(document).on('click', '.test', function(e) {
    e.preventDefault();
    self.doAlert();
  });

#1


3  

It's because you've defined do_alert() within the scope of the helpers function.

这是因为你已经在helpers函数的范围内定义了do_alert()。

To fix this you will need to move that function to within scope of the object you return. You could either put it at root level of the object (which would work fine, but could get messy if you have a lot of 'helper' functions) or you could nest it within your helpers property if you define that as another object. Personally, I'd use the latter to have some semblance of organisation. Try this:

要解决此问题,您需要将该函数移动到返回的对象的范围内。您可以将它放在对象的根级别(这可以正常工作,但如果您有很多'帮助'函数可能会变得混乱),或者如果您将其定义为另一个对象,则可以将其嵌套在helpers属性中。就个人而言,我会用后者来表达一些组织。尝试这个:

var setup_system = (function($) {
  "use strict";

  return {
    init: function() {
      this.logout_users();
    },
    logout_users: function() {
      var _obj = this;
      $(document).on('click', '.test', function(e) {
        e.preventDefault();
        _obj.helpers.do_alert();
      });
    },
    helpers: {
      do_alert: function() {
        alert('foo');
      }
    }
  };
})(jQuery);

jQuery(function() {
  setup_system.init();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">Click me</div>

Note that I cached the reference to the object as _obj outside of the click handler, as within that block this will refer to the clicked .test element.

请注意,我将对象的引用缓存为单击处理程序之外的_obj,因为在该块中,这将引用单击的.test元素。

#2


1  

Do_alert function exist only in helpers method, so you can't access to it.

Do_alert函数仅存在于helpers方法中,因此您无法访问它。

You need to declare your function directly in the logout_user method or outside, try this :

您需要直接在logout_user方法或外部声明您的函数,请尝试以下方法:

 var setup_system = (function ($) {
 "use strict";

 return {

     init: function () {
        this.logout_users();
        this.helpers();
     },

    logout_users: function() {

            function do_alert(){
            alert();
        }

        $(document).on('click', '.test', function(e){
            e.preventDefault();
            do_alert();
        });
    },

    helpers: function () {

        function do_alert(){
            alert();
        }
       }

   };
})(jQuery);

jQuery(document).ready(function () {
    setup_system.init();
});

#3


0  

When helpers is invoked by the initfunction, all that is happening is that do_alert is being declared. But function declarations are hoisted to the top of their lexical scope. The lexical scope of do_alert is the scope defined by the helpers function. Therefore, do_alert is not accessible outside of helpers function.

当initfunction调用helper时,所有发生的事情都是声明do_alert。但是函数声明被提升到它们的词法范围的顶部。 do_alert的词法范围是助手函数定义的范围。因此,在辅助函数之外无法访问do_alert。

A couple things you could do. The first one that comes to mind is: you could have the helpers method define a method called do_alert on the object being returned rather than merely declaring a function, like so:

你可以做几件事。首先想到的是:你可以让helpers方法在被返回的对象上定义一个名为do_alert的方法,而不仅仅是声明一个函数,如下所示:

   helpers: function() {
    this.doAlert = function() {
        alert();
    }
}

When your doAlert() is invoked by the event handler passed to jQuery, it will not work with the above solution. Instead you will need to make sure you call that doAlert on the returned object in that event handler. This is how you can do that:

当你的doAlert()被传递给jQuery的事件处理程序调用时,它将无法使用上述解决方案。相反,您需要确保在该事件处理程序中的返回对象上调用该doAlert。这是你如何做到这一点:

 logout_users: function() {
    var self = this;
  $(document).on('click', '.test', function(e) {
    e.preventDefault();
    self.doAlert();
  });