简体   繁体   English

.prototype如何在JavaScript中工作?

[英]How does .prototype works in JavaScript?

I just started a tutorial in game development using JavaScript and HTML5. 我刚刚开始使用JavaScript和HTML5进行游戏开发的教程。 javascript.prototype has come into play many times. javascript.prototype已经多次发挥作用。 I can not really understand how it works. 我真的不明白它是如何工作的。 Here is a link I found with a good explanation, but I am still a little bit confused 这是我找到的链接,上面有很好的解释,但我仍然有些困惑

How does JavaScript .prototype work? JavaScript .prototype如何工作? .

Can anyone explain this please? 谁能解释一下? Here is an example of my code: 这是我的代码示例:

 function Enemy(){
        this.srcX = 140;  //gets the location of enemy in x and Y here
        this.srcY = 600;
        this.width = 45;//gets the enemies width and height here
        this.height = 54; 
        this.drawX = randomRange(0, canvasWidth  - this.width);
        this.drawY = randomRange(0, canvasHeight - this.height); 
        this.centerX = this.drawX + (this.width / 2);
        this.centerY = this.drawY +(this.height / 2);

        //this.targetX = this.centerX;
        //this.targetY = this.centerY;
        //this.randomMoveTime = randomRange(4000,10000);
        this.speed = 1;
        //var that = this;
        //this.moveInterval = setInterval(function() {that.setTargetLocation();},that.randomMOveTime);
        this.isDead = false;
    }

    Enemy.prototype.update = function() {
        //this.checkDirection();
         this.centerX = this.drawX + (this.width / 2);
        this.centerY = this.drawY + (this.height / 2);
    }

I think the biggest confusion over prototype inheritance is that that objects inherit from their contructor's prototype via their internal [[Prototype]] property. 我认为对原型继承的最大困惑是对象通过其内部[[Prototype]]属性从其构造方法的原型继承。 Both are referred to as "the prototype". 两者都称为“原型”。 All functions have a default prototype property that is an empty object. 所有函数都有一个默认的原型属性,该属性是一个空对象。

In your case: 在您的情况下:

function Enemy(){
        this.srcX = 140;  //gets the location of enemy in x and Y here
        this.srcY = 600;
        ...
}

is a function that, when called with new , acts as a constructor that assigns properties to a new instance of itself. 是一个函数,当使用new调用时,它充当将属性分配给自身的新实例的构造函数。 That instance has an internal [[Prototype]] property that points to Enemy.prototype . 该实例具有一个内部[[Prototype]]属性,该属性指向Enemy.prototype

Then: 然后:

Enemy.prototype.update = function() {
    //this.checkDirection();
     this.centerX = this.drawX + (this.width / 2);
    this.centerY = this.drawY + (this.height / 2);
}

This assigns a new property to Enemy.prototype that is a function. 这会将新属性分配给函数Enemy.prototype So that puts an update property on the inheritance chain of all instances of Enemy , so they all inherit an update method. 因此,将update属性放在Enemy的所有实例的继承链上,因此它们都继承了update方法。

So: 所以:

var enemy0 = new Enemy();
var enemy1 = new Enemy();

typeof enemy0.update // function
typeof enemy1.update // function

enemy0.update === enemy1.update  // true

The test can only be true if both expressions reference the same object (ie the function assigned to Enemy.prototype.update ). 仅当两个表达式都引用相同的对象(即,分配给Enemy.prototype.update的函数)时,测试才能为true。

You can continue to add properties and methods to Enemy.prototype and they will be inherited by all instances, even those already constructed. 您可以继续向Enemy.prototype添加属性和方法,它们将被所有实例继承,即使是已经构造的实例也是如此。 But if you change it to a different object: 但是,如果将其更改为其他对象:

Enemy.prototype = {update:function(){/*new update function*/}};

then old instances will still have the old [[Prototype]] and new instances will have the new one. 那么旧实例仍将具有旧的[[Prototype]]而新实例将具有新的实例。

There are a million articles on the web about javascript's prototype inheritance, read a few and play with it, ask more questions if you have them. 网上有上百万篇关于javascript的原型继承的文章,请阅读并使用它,并提出更多问题。 :-) :-)

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

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