Search code examples
javascriptgraphscene

While perusing "effective javascript" book, do not understand Item 38(scene)


I love the book 'Effective Javascript 68 ways to harness.....'. Although at this point, I feel it's bit out of my scope(start talking about canvas and others). And I ran into Item 38: Call Superclass Constructors from subclass constructors. And while getting graph on web is not the point of the item(or maybe it is), I got curious and wanted it to work but one thing I don't understand is what I need to pass as 'scene' when I construct the Actor. Is this example from some other library that I am missing or am I missing some chunk of code to construct 'scene' object... ?

Below is the code.

     function Scene(context, width, height, images) {
         this.context = context;
         this.width = width;
         this.height = height;
         this.images = images;
         this.actors = []; 
     }

     Scene.prototype.register = function(actor) {
        this.actors.push(actor);
     };

     Scene.prototype.unregister = function(actor) {
        var i = this.actors.indexOf(actor);
        if ( i >= 0 ) {
             this.actors.splice(i,1);
        }
     };

     Scene.prototype,draw = function() {
        this.context.clearRect(0,0, this.width, this.height);
        for (var a = this.actors, i = 0, n = a.length; i < n; i++) {
            a[i].draw();
        }

     };

     function Actor(scene,x,y) {
          this.scene = scene;
          this.x = x;
          this.y = y;
          scene.register(this);
     } 

     Actor.prototype.moveTo = function(x,y) {
        this.x = x;
        this.y = y;
        this.scene.draw();
     };

     Actor.prototype.exit = function() {
        this.scene.unregister(this);
        this.scene.draw(); 
     };

     Actor.prototype.draw = function() {
        var image = this.scene.images[this.type];
        this.scene.context.drawImage(image, this.x, this.y);
     };

     Actor.prototype.width = function() {
        return this.scene.images[this.type].width;
     };

     Actor.prototype.height = function() {
        return this.scene.images[this.type].height;
     };

     function SpaceShip(scene,x,y) {
        Actor.call(this.scene,x,y);
        this.points = 0; 
     }

     SpaceShip.prototype = Object.create(Actor.prototype);
     SpaceShip.prototype = new Actor('jot', 3,2);

     SpaceShip.prototype.type = "spaceShip";

     SpaceShip.prototype.scorePoint = function() {
         this.points++;
     };

     SpaceShip.prototype.left = function() {
        thils.moveTo(Math.max(this.x - 10, 0 ), this.y);
     };

     SpaceShip.prototype.right = function() {
         var maxWidth = this.scene.width - this.width();
         this.moveTo(Math.min(this.x + 10, maxWidth), this.y);
     };

Solution

  • The scene parameter in the Actor function obviously refers to an instance of Scene. This example has some similarities to a pattern called the Mediator Pattern, which is described here: http://www.dofactory.com/javascript/mediator-design-pattern

    By the way your code has some flaws:

     //the first argument of the `call` function is the scope, in this case `this` which refers to the newly created SpaceShip instance
     function SpaceShip(scene,x,y) {
        Actor.call(this,scene,x,y);
        this.points = 0; 
     }
    
     SpaceShip.prototype = Object.create(Actor.prototype);
     //you do not need this line
     //SpaceShip.prototype = new Actor('jot', 3,2);
     //but you could add this line to set the constructor property to SpaceShip
     SpaceShip.prototype.constructor = SpaceShip;