Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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私有方法问题_Javascript_Oop - Fatal编程技术网

Javascript私有方法问题

Javascript私有方法问题,javascript,oop,Javascript,Oop,我想下面的错误对于不是新手的人(像我一样)来说是很容易纠正的 有人能告诉我为什么在下面的代码中调用“this.slideNext()”不起作用吗。显然“this.slideNext()”不是一个函数 function ScoopAnimation(_path, _start, _end, _delay) { this.start = _start this.end = _end; this.delay = _delay; this.path = _path

我想下面的错误对于不是新手的人(像我一样)来说是很容易纠正的

有人能告诉我为什么在下面的代码中调用“this.slideNext()”不起作用吗。显然“this.slideNext()”不是一个函数

function ScoopAnimation(_path, _start, _end, _delay) {

    this.start = _start
    this.end = _end;
    this.delay = _delay;
    this.path = _path
    this.currentFrame = _start;

    this.slideNext() = function() {
        this.currentFrame++;
        console.log('  next this.currentFrame  : ' + this.currentFrame);
    }

    this.start = function() {
        console.log('next this.start()   : ' + this.currentFrame);
        //THE NEXT LINE CAUSES THE ERROR!
        this.slideNext()
    }

    this.start();

}

不,你说的“坏的那一条”实际上是正确的。 接下来,您将尝试执行slideNext函数,然后为结果分配一个函数。应该是这样,

this.slideNext = function (){
    this.currentFrame ++;
    console.log('  next this.currentFrame  : ' +this.currentFrame );
}   

希望我帮了忙

我可能错了,但它不应该被定义为:

// defined without brackets
this.slideNext = function (){
    this.currentFrame ++;
    console.log('  next this.currentFrame  : ' +this.currentFrame );
    } 

根据调用函数的方式,此
对每个函数具有不同的引用/上下文。在您的代码段中,您正在调用
start
函数(),该函数(像这样调用)将引用其
上下文变量中的
全局对象
,用于非ES5 strict和ES5 strict中的
未定义

要解决这个问题,您可以将“outer”
this
的引用存储在局部变量中,如

var myScope = this;
然后在访问外部作用域所需的任何其他函数上下文中使用
myScope
而不是
this

myScope.slideNext();
另一个选项是使用ES5
Function.prototype.bind
绑定函数的上下文。这看起来像:

this.start = function() {
    console.log('next this.start()   : ' + this.currentFrame);
    //THE NEXT LINE CAUSES THE ERROR!
    this.slideNext()
}.bind(this);

现在,我们将
this
的当前值绑定到
start
函数的上下文。现在,您可以继续在函数中使用
。请注意,这只适用于支持ES5的js引擎,或者您已经加载了某种ES5垫片脚本。

如果您不打算将ScoopANimation用作构造函数,那么我个人会放弃使用“this”:

function ScoopAnimation(_path, _start, _end, _delay) {

  var start = _start,
      end = _end,
      delay = _delay,
      path = _path,
      currentFrame = _start;

    function slideNext() {
      currentFrame++;
      console.log('  next this.currentFrame  : ' + currentFrame);
    }

    function start() {
      console.log('next this.start()   : ' + currentFrame);
      //THE NEXT LINE CAUSES THE ERROR!
      slideNext()
    }

    start();
}

看起来你的分号不见了。如果添加分号时错误仍然存在,您可以尝试一下吗?此外,你能发布错误消息吗?您可以在Firefox的Web控制台上看到它,例如.Gotcha。分号不是问题所在。错误消息为“this.slideNext不是函数”。Japrescott的解决方案消除了错误。看起来我有一套额外的副焓;)对于“赏金猎人”来说,你的速度还不够快,但既然你已经获得了积分,那就来吧!不,我不在乎分数,但是如果我的答案是正确的,并且我花了很多时间来写它,想要一些认可对我来说似乎是合法的:)(既然你不在乎分数,为什么不把它们捐给我?:P)sry,你是对的。我尽量不鼓励“积分竞赛”。但你是对的。你的答案很好=>upvotejaprescott,谢谢,这很有效(唉,我刚刚注册,没有足够的分数来投票。否则,我会的)。谢谢你的评论。它帮助我更好地理解Javascript中的“this”