简体   繁体   English

这是常规的构造函数吗?

[英]Is this a regular constructor function?

So I've been looking at a script for a game which I've downloaded. 所以我一直在看我下载的游戏脚本。 The thing I'm unsure about is Bullet(I) is never instantiated within the script (ie var x = new Bullet). 我不确定的是Bullet(I)从未在脚本中实例化(即var x = new Bullet)。 The tutorial refers to this as a constructor function though. 本教程将其称为构造函数。 What is going on? 到底是怎么回事?

It looks as though the Bullet constructor is taking a parameter and adding properties etc to it. 看来Bullet构造函数正在接受参数并为其添加属性等。 But nowhere in the script is Bullet ever instantiated - so it cannot be? 但是脚本中的任何地方都没有实例化Bullet-不可能吗?

var playerBullets = [];

    function Bullet(I) {
      I.active = true;
      I.xVelocity = 0;
      I.yVelocity = -I.speed;
      I.width = 3;
      I.height = 3;
      I.color = "#000";

      I.inBounds = function() {
        return I.x >= 0 && I.x <= CANVAS_WIDTH &&
          I.y >= 0 && I.y <= CANVAS_HEIGHT;
      };

      I.draw = function() {
        canvas.fillStyle = this.color;
        canvas.fillRect(this.x, this.y, this.width, this.height);
      };

      I.update = function() {
        I.x += I.xVelocity;
        I.y += I.yVelocity;

        I.active = I.active && I.inBounds();
      };

      I.explode = function() {
        this.active = false;
        // Extra Credit: Add an explosion graphic
      };

      return I;
    }

This code is used later in the script and as far as I can see, this has to be the relevant part of the script that is using the Bullet(I) function? 此代码在脚本的后面使用,据我所知,这一定是脚本中使用Bullet(I)函数的相关部分吗?

playerBullets.forEach(function(bullet) {
    bullet.update();
});

I wouldn't personally call that a constructor in the usual sense where, as you correctly state, you would use it like var myBullet = new Bullet() . 我不会亲自将其称为通常意义上的构造函数,在正确意义上,您将像var myBullet = new Bullet()那样使用它。

It does however do a similar job of "constructing" and object by using javascripts loose typing to be able to add properties and methods to an object at run-time. 但是,通过使用javascript松散类型,它能够在运行时向对象添加属性和方法,从而完成了“构造”和对象的类似工作。 This method would be used like: 该方法的用法如下:

var myBullet = {}; // blank object
Bullet(myBullet);
// myBullet now has methods .draw, .active etc

As I have discovered the code you have downloaded came from this tutorial and the method Bullet is indeed called on line 167 of index.html. 当我发现您下载的代码来自本教程时 ,确实在index.html的第167行调用了Bullet方法。 It configures a bullet when the player shoots: 它在玩家射击时配置子弹:

player.shoot = function() {
      Sound.play("shoot");

      var bulletPosition = this.midpoint();

      playerBullets.push(Bullet({ //<-- here
        speed: 5,
        x: bulletPosition.x,
        y: bulletPosition.y
      }));
    };

It appears that Bullet is taking a (presumably empty) object as a parameter and then turning it into a an object of Bullet "class" (yes, I know javascript doesn't have classes, but you know what I mean). 看来Bullet将一个(可能是空的)对象作为参数,然后将其变成Bullet“类”的对象(是的,我知道javascript没有类,但是您知道我的意思)。 I suspect the reason for this is to ensure objects are reused rather than being created and destroyed. 我怀疑这样做的原因是为了确保对象被重用,而不是被创建和销毁。 This means less garbage collection takes place and so the game runs more smoothly 这意味着更少的垃圾收集发生,因此游戏运行更加流畅

It seems, that it takes an -probably- object, namely "I", as an input, and attaches stuff to it and returns that. 看来,它采用了一个可能的对象(即“ I”)作为输入,并将内容附加到该对象上并返回该对象。 No need to instantiate Bullet itself. 无需实例化Bullet本身。 You only need proper I(nput)objects. 您只需要适当的I(nput)对象。 At best it should be called something like "builder function". 充其量应该称为“构建器功能”。

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

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