Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在函数中获得正确的作用域(不使用that=this)_Javascript_Angularjs - Fatal编程技术网

Javascript 在函数中获得正确的作用域(不使用that=this)

Javascript 在函数中获得正确的作用域(不使用that=this),javascript,angularjs,Javascript,Angularjs,我正在尝试修复一个函数,我必须在这里使用它,而不必使用that=this(或self=this,因为有些人喜欢使用)。这是一个范围问题,但我不知道如何解决它,我想养成不使用that=this的习惯。所以函数都在一个返回(角度工厂)中,我在引用另一个函数时遇到困难。让我告诉你我的意思: return { loadStates: function() { var that = this;

我正在尝试修复一个函数,我必须在这里使用它,而不必使用that=this(或self=this,因为有些人喜欢使用)。这是一个范围问题,但我不知道如何解决它,我想养成不使用that=this的习惯。所以函数都在一个返回(角度工厂)中,我在引用另一个函数时遇到困难。让我告诉你我的意思:

  return {
            loadStates: function() {

                     var that = this;
                     //chgeck if is loaded module, then change and fire callback
                     var currentModules = moduleHolder.getModules();
                     if (currentModules[name]) {
                        //works here
                         this.prepState();

                     } else {
                         //module cannot be found check for 5 seconds
                         $log.warn("Requesting " + name + "...");
                         var timeToCheck = true;
                         setTimeout(function() {
                             timeToCheck = false;
                         }, 5000);
                         var check = {
                             init: function() {
                                 check.checkAgain();
                             },
                             checkAgain: function() {
                                 if (timeToCheck) {
                                     if (currentModules[name]) {
                                        //but not here
                                         that.prepState();
                                     } else {
                                         //still doesn't exists
                                         setTimeout(check.checkAgain, 200);
                                     }
                                 } else {
                                     //doesn't exist after 5 seconds
                                     $log.error("Requested module (" + name + ") could not be found at this time.");
                                 }
                             }
                         };
                         check.init();
                     }

                 },
                 prepState: function() {

                 }

             }
因此,在顶部,如果它找到currentModule[name],我可以使用this.prepState()命令,它可以正常工作。但是在计时函数中,我不能使用this anythis,因为它在不同的范围内,所以我暂时通过设置a that=this up top来解决这个问题,但是我想看看我是否不能使用这个方法。如果不使用that=this,如何绕过这个问题?谢谢

这是一个范围问题

不,不是<代码>此和范围基本上互不相关。(目前,ES6的箭头函数将改变这一点。)这是函数如何调用的问题

如果将函数引用传递给稍后将调用它的对象,除非传递给它的对象在调用它时有一种方法可以告诉它使用什么
this
,否则将使用
this
调用函数,而不是引用希望它引用的内容

通过使用
function#bind
,您可以获得一个新函数引用,该引用将使用正确的
this
调用原始函数:

var usesCorrectThis = originalFunction.bind(valueForThis);
例如,假设我有:

var check = {
    name: "Fred",
    sayHello: function() {
        console.log("Hi, I'm " + this.name);
    }
};
如果我这样做:

check.sayHello();
一切都很好:调用函数作为从属性检索它的表达式的一部分,告诉JavaScript引擎在调用过程中使用对象作为
this

但是,如果我这样做:

setTimeout(check.sayHello, 0);
…这不正确,因为当
setTimeout
调用函数时,它没有为
this
使用正确的值

因此,我可以使用
函数#bind
来解决以下问题:

setTimeout(check.sayHello.bind(check), 0);
更多(在我的博客上):


有不同的方法可以做到这一点。 一种方法是使用bind函数

 var checkInitBindFn = check.init.bind(this);
 checkInitBindFn();
其次,你也可以使用call和apply

check.init.call(this);
check.init.apply(this);
像这样,你可以用这个代替那个。
在线查看完整的api文档…

这不是范围问题。如果要避免
self=this
,则始终可以按对象引用函数。使代码更干净,而且由于angular中的工厂是单例的,所以您不会浪费内存

angular.module('myApp').factory('myFactory', function ($timeout) {

  var myFactory = {
    loadState: function () {

      $timeout(function () {
        myFactory.check();
      }, 500);

    },
    check: function () {

    },
  };

  return myFactory;

}); 

我很难将此应用到我的示例中。我明白你在这里说的话,但是我怎么称呼等价的check.hello.bind(check)呢?