简体   繁体   English

理解原型类的范围

[英]Trouble understanding scope of Prototype Class

I'm currently writing a Scene structure for a game I'm making in javascript with the createjs framework. 我目前正在使用createjs框架在javascript中编写游戏的Scene结构。 The problem I'm running into is properly referencing the original class in the prototypal functions. 我遇到的问题是正确引用原型函数中的原始类。 I'm relatively new to javascript and this is the first time I've had to use prototype. 我对javascript比较陌生,这是我第一次不得不使用原型。 My current code is as follows: 我当前的代码如下:

function Intro(p, c){
    this.parent = p;
    var s = new createjs.Stage(c);
    this.stage = s;

    this.queue = new createjs.LoadQueue(false);
    this.queue.installPlugin(createjs.Sound);
    this.queue.addEventListener("complete", this.handleComplete);
    this.queue.loadManifest([{id:"bg", src:"images/Intro/intro_background.png"}]);
}
Intro.prototype.handleComplete = function(event){
    console.log("queue completed - handling...");
    var q = event.target;
    var bg = new createjs.Bitmap(q.getResult("bg"));
    this.stage.addChild(bg);
    this.stage.update();
}

When I get to 当我到达

this.stage.addChild(bg); this.stage.addChild(bg);

it appears to lose scope and I get "cannot call method 'addChild' of undefined. 它似乎失去作用域,我得到“无法调用未定义的方法'addChild'。

Any help would be greatly appreciated! 任何帮助将不胜感激! -xv -xv

You can fix your scope problem by changing 您可以通过更改解决范围问题

this.queue.addEventListener("complete", this.handleComplete);

to

this.queue.addEventListener("complete", this.handleComplete.bind(this));

so that the scope of the bound function is this . 因此绑定函数的范围是this

BTW, you might be interested by my game in which I override createjs classes ( https://github.com/Canop/SpaceBullet/tree/master/src ). 顺便说一句,您可能对我重写createjs类( https://github.com/Canop/SpaceBullet/tree/master/src )的游戏感兴趣。 I think I handled this problem in a clean way. 我想我以干净的方式处理了这个问题。

If you invoke function in JS, it'll be bound dynamically. 如果在JS中调用函数,它将被动态绑定。 What value will be bound to the this depends on the way you invoke it, whether function is called as a constructor and whether your code is running in strict mode. this绑定的值取决于您调用它的方式,是否将函数作为构造函数调用以及您的代码是否在严格模式下运行。

In your case, the following line: 对于您的情况,以下行:

this.queue.addEventListener("complete", this.handleComplete);

makes your function to run with this bound to the global object (in the web browser, global object is a window object) or, if in strict mode, this will be undefined. 使您的函数与this绑定到全局对象一起运行(在Web浏览器中,全局对象是window对象),或者,如果在严格模式下, this函数将是未定义的。

As @dystroy suggested, use bind() method to change this behaviour. 如@dystroy所建议,请使用bind()方法更改此行为。 Calling: 致电:

this.queue.addEventListener("complete", this.handleComplete.bind(this));

causes binding this inside the handleComplete() to the same this as in Intro . 使结合this内部handleComplete()到相同的this如在Intro


If you want to understand it in more detail. 如果您想更详细地了解它。 I strongly suggest reading Dmitry Soshnikov's blog . 我强烈建议阅读Dmitry Soshnikov的博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM